|
|
|
|
![]() ![]() |
Oct 27 2006, 12:11 PM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 211 Joined: 15-September 06 Member No.: 30,028 |
Quick into on .htaccess
.htaccess (note the dot) is a text file, each command is meant to be placed on a single line, so if your text editor has word-wrap, make sure it is turned off. When you upload it to your server, make sure it is uploaded in ASCII mode rather than binary. What is mod_rewrite? mod_rewrite is a apache modal that allows you to rewrite URLs at a server level. For example, when a user requests the page http://www.domain.com/index/disks/ mod_rewrite could rewrite the URL so that the sever really gives this page http://www.domain.com/index.php?page=disks Why should I use mod_rewrite? The problem is that search engines used to not search urls when they had characters like ? and = in. These characters usually occurred when you had a dynamic website, with URLs like http://www.domain.com/index.php?page=disks&prod=246, it created a nightmare in SEO (Search Engine Optimization). What they used do was chop off the URL after the none standard character, so if you had a set of URLs: http://www.domain.com/index.php?page=disks http://www.domain.com/index.php?page=paper http://www.domain.com/index.php?page=somepage Every URL would end up as http://www.domain.com/index.php Thankfully, most search engines can crawl dynamic URLs, unfortunately, they don't do it as efficiently as they do it with static URLs and in some cases they don't crawl every page. It also has benefits like allowing you to change the language the site runs on. For example, it you converted your site from asp to PHP, no urls would need to change, so you wouldn't have any problems with 404 errors How do I use mod_rewrite? The first thing you need to do is check you have mod_rewrite installed on your server. Create a php page that looks like so: CODE <?php phpinfo(); ?> Upload it to your server. Load the page in your web browser and perform a search for mod_rewrite, you should find it under the "Loaded Modules" section. If you can't find it, you will have to contact your hosting company and ask them to add it. Note: Trap17 has this modal, so you don't need to do this if you are hosted at Trap17. Basic Example The most basic form of mod_rewrite is like this. CODE RewriteEngine on RewriteRule ^apage.php$ anotherpage.php This would redirect someone who loaded apage.php to anotherpage.php, without changing the URL that they see in their browser address bar RewriteEngine on Turns the Rewrite engine on, without this we could do nothing RewriteRule This is the name of the command ^apage.php$ The ^ and the $ are the beginning and the end of the anchors respectively. They are called anchors because they "anchor" the RewriteRule in a specific position in the URL string. anotherpage.php This is the page that the URL is re-directed to. More Complex Examples Example One CODE RewriteEngine on RewriteRule ^cms/([^/]+)/?$ index.php?page=$1 [L] This will redirect a URL like: http://www.domain.com/cms/disks to http://www.domain.com/index.php?page=disks ^cms/ If the URL requested page begins with cms/ ([^/]+) Anything within brackets is assigned to a variable like $1 or $2 etc. This gets one or more characters that isn't a forward slash, and assign it to a variable. /?$ This makes sure that the requested URL ends with a / or nothing else. In more detail, the / is the character that it checks if it ends with, the ? makes the character optional, so it can be a forward slash or nothing, and the $ is the end of the anchor. index.php?page=$1 This redirects the page to index.php?page=$1. The $1 is the text that is in ([^\]+) . If you had more than one set of brackets, like ^cms/([^\])/([^\]+)/?$, the second set of brackets would have the backreference $2 [L] This makes it so that if the RewriteRule is successful, it is the last rewrite rule to be executed. Example Two CODE RewriteEngine on RewriteRule ^download/([^/]*\.)(zip|exe|bat)$ files/$1$2 This will redirect a request for http://www.domain.com/download/something.extension to http://www.domain.com/files/something.extension RewriteRule ^download/ The URL must start with download/ ([^/]*\.) This matches and assigns to a variable anything that isn't a forward slash, and ends with a ".", the "\" before the period is required to signify that the period is a character rather than a command. (The period on its own matches any character, not what we want!). Notice we have uses an asterisk (*) instead of a plus (+), this means get zero or more characters rather than one or more characters (zip|exe|bat)$ This matches a zip or exe or bat, and assign it to a variable files/$1$2 This redirects the request to real file using the variables assigned. Example Three If you have a dynamic URL that contains numeric sections, like so: http://www.domain.com/index.php?id=231&page=1 If you don't want any non numeric character, you can use this CODE RewriteEngine on RewriteRule ^articles/([0-9]+)/([0-9]+)/?$ index.php?id=$1&page=$2 This would re-direct a page request like: http://www.domain.com/articles/231/1/ to http://www.domain.com/index.php?id=231&page=1 The only new part in this is this: ([0-9]+) This gets one or more numbers between 0 and 9, you can also specify one or more lower case characters using ([a-z]+), if you wanted uppercase, you would change it to ([A-Z]+). Tutorial Copyright Jacob Essex 2006 This may not be copied or mirrored without permission ------------------------------------------------- Any comments on this would be appreciated, as this is the second tutorial I have ever written, so I would welcome constructive criticism. This post has been edited by Yacoby: Oct 31 2006, 11:10 AM |
|
|
|
![]() ![]() |
Similar Topics
| Topics | Topics | |
|---|---|---|
|
|
|
Lo-Fi Version | Time is now: 12th October 2008 - 11:05 PM |