Getting Started

Information and tutorials covering what you can and can't do with mod_rewrite, regular expressions, creating rules, htaccess placement.

Getting Started

Postby Mad Mod_Rewriter » Thu Jan 19, 2006 1:00 pm

Finally Getting Started
Mod-Rewrite URL Redirection

Creating Some Simple Rules for URL Redirection


Well, if you've made it this far, you are probably still interested, and would really like to get started with your own rules. So, in the interest of not leaving you disappointed, I'll walk you through a few rules that would work with the example site and it's structure. (Make sure you read the whole page before you go and 'try' anything, there are some important tips at the bottom.)

In the first example we will pretend my htaccess is in the main folder of the example site. The structure looks like this:

www.yoursite.com contains
1. index.html
2. information-1.html
3. pages
(this is a sub-folder)
4. more-pages (this is also a sub-folder)
5. htaccess

For this example assume I have moved the pages from the folder 'more-pages' to the folder 'pages', and my goal is to redirect everyone who tries to visit the folder 'more-pages' to the correct page in the folder 'pages' and I want to let them know I am doing it.

(In short if you ask for either of the pages in 'more-pages' I am going to redirect you to the same file in 'pages')


The new structure looks like this:
www.yoursite.com has a total of five items:
1. index.html
2. information-1.html
3. pages
(this is a sub-folder)
4. more-pages (this is also a sub-folder)
5. htaccess

The sub-folder 'pages' has a total of four items:
1. information-2.html
2. information-3.html
3. information-4.html
4. information-5.html


The sub-folder 'more-pages' has a total of 0 items.


The address, or URL of the pages this rule will work for, looks like this in a browser:

http://www.yoursite.com/more-pages/information-4.html [OR]
http://www.yoursite.com/more-pages/information-5.html


The rule I would use in my htaccess is as follows:

RewriteRule ^more-pages/([a-z]+)-([0-9])\.html$ /pages/$1-$2.html [R=301,L]

(The first time you read the break down of the rules through, just read the sentences on every-other line. This is what the full expression actually says.)


The left side of the rule breaks down to (everything until the $):

^
Start matching here.

more-pages/
If a request starts with 'more-pages/'

([a-z]+)
and it has any letters in a row (string), store them as a variable

-
then after the letters if it has a '-' in it,

([0-9])
and also has a number after the '-', find the number from 0 to 9, and store it as a variable.

\.

Match the .(dot), do not use it as a special character.


html
Make sure it ends in html

$
Stop matching here.


The right side of the rule breaks down to (everything after the $):

/pages

Go from www.yoursite.com to www.yoursite.com/pages


/$1

Take the letters stored and put them here, after a /.

-

Put a '-' here.


$2

Take the number stored and put it here.


.html

Add .html at the end.


[R=301,L]
R
Redirect the browser, so everybody can see.

=
Attach a code to the redirect command.

301
Tell every browser and search engine this is a permanent move.

,
There is another directive.

L
Stop processing now.

The above rule will work for both of the pages I moved, but it would not work for the page below, because with out the '-' the page does not match the pattern I specified.

http://www.yoursite.com/more-pages/information4.html

In the second example, I will do the same thing from within the 'more-pages' folder, please note what changes, and what does not change with the rule.

My new structure looks like this:
www.yoursite.com has a total of four items:
1. index.html
2. information-1.html
3. pages
(this is a sub-folder)
4. more-pages (this is also a sub-folder)


The sub-folder 'pages' has a total of four items:
1. information-2.html
2. information-3.html
3. information-4.html
4. information-5.htm


The sub-folder 'more-pages' has a total of 1 item.
1. htaccess

The rule for rewriting the sub-folder is similar, but slightly different.

RewriteRule ^([a-z]+)-([0-9])\.html$ /pages/$1-$2.html [R=301,L]

The main difference is I have had to remove the 'more-pages/' portion from the left side of the rule. This is because I am already in the 'more-pages' folder, so I have to start the rule from there.

The right side of the rule stays exactly the same, because after the rewrite takes place the request is sent back and reprocessed, and if '/pages' is not inlcuded, the server will look for 'http://www.yoursite.com/information-4.html' (or 5.html), which does not exist.


Some final things to check before you begin URL rewriting: (If you do not know how to do any of these, or do not have access, you may have to check with your hosting company.)

1. Make sure your server is configured to AllowOverride FileInfo, or All, not None.
2. Make sure you have Options +FollowSymLinks
3. Make sure every htaccess file you use contains RewriteEngine on


Your actual htaccess file may look like this:


Options +FollowSymLinks

RewriteEngine on
RewriteRule ^more-pages/([a-z]+)-([0-9])\.html$ /pages/$1-$2.html [R=301,L]



Now that you have a base for mod-rewrite URL redirection, and should be able to at least have some idea of what a rule is doing - Have Fun, and please keep in mind you have only just scratched the surface of the power and application of mod-rewrite.

Apache Mod-Rewrite Documentation
Apache URL Rewriting Guide
Mad Mod_Rewriter
 
Posts: 26
Joined: Thu Jan 12, 2006 1:53 pm
Location: The Office

Return to The Basics of Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 1 guest