RewriteRule question

Hi,

I’d like to use a RewriteRule that would convert http://www.mysite.com/the_work to http://www.mysite.com/index.php?page=the_work.

The reason is I’d like to be able to use $_GET[‘page’] to identify the current page while offering a SEO-friendly URL to the page.

What I planned to do is like this:

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^the_work/ /the_work [R]
RewriteRule ^the_work /index.php?page=the_work

Is this the right way of doing this?

Many thanks in advance!

OC,

No problem! That’s what I’m here for (helping you LEARN).

Regards,

DK

Works like a charm! Thanks DK! Thanks inclick for taking the time to answer!

Thank you guys! I knew I’ll be able to find the answer here! :slight_smile:

I will be playing around with the suggested solution to see what sticks. Thanks again!

OC,

First, your code is deficient in that it requires that the {REQUEST_URI} match BOTH the_work/ and the_work successively (those are ANDed in your code) then the page will be redirected to /index.php and then be ANDed with the following mod_rewrite code.

THe problems can easily be resolved by using the optional notation and using the Last flag to terminate the block statement (as you would with ; or }). Sometimes, too, Apache does NOT like to see the leading / in the redirection as it confuses the DocumentRoot with the server’s physical root - but that’s with an Apache that’s ready to fall over.

RewriteEngine on
RewriteRule ^the_work/? index.php?page=the_work [L]

inclick,

Thanks for the help but your code will generate an infinite loop on Apache 1.x and will not work at all on Apache 2.x.

The Apache version problem is caused by the leading / in your regex which is required for a DocumentRoot in Apache 1.x and will prevent a match with Apache 2.x. If you’re not sure which version you’re using, either don’t use the start anchor (^ - which you didn’t use) or make it optional (/?).

The “loopy” problem is that your redirection will be matched on the next pass through mod_rewrite, i.e., ([^/]*) will match index.php and loop.

To me, using a “catch-all” like ([^/]) is almost as bad as using the dreaded :kaioken: EVERYTHING :kaioken: atom ( the (.) ) as it only eliminates the / from the list of matching characters. BE SPECIFIC in what you match to avoid problems (and check that the regex will not match the redirection)!

Regards,

DK

RewriteEngine On
RewriteRule /([^/]*)$ /index.php?page=$1 [L]