Forum Moderators: phranque
I had been tasked to simplify and clean-up the url of our website that currently is in the form:
http://www.example.com/profile.php?action=view&userID=234
to something like:
http://www.example.com/user/234
We would like to implement a solution in such a way that we would only manipulate via mod_rewrite
and the code would stay the same. That is, when somebody directly access:
http://www.example.com/profile.php?action=view&userID=234
he/she would be automatically be redirected to the equivalent
http://www.example.com/user/234
I'm a newbie in mod_rewrite but I tried my best to solve the problem using the following in .htaccess:
####### START: Mod_rewrite #######
# Enable mod_rewrite
Options +FollowSymLinks
# Start rewrite engine
RewriteEngine on
RewriteRule user/(.*)$ /profile.php?action=view&userID =$1 [R=301,L]
####### END: Mod_rewrite #######
It partially solved the problem since the old URL:
http://www.example.com/profile.php?action=view&userID=234
is still accesible as it is, the only difference is that the goal url could now be accessed as well(need to be visited directly):
http://www.example.com/user/234
I found a way to enable logging for mod_rewrite and I could sense that the RewriteRule above is being matched to almost all
URL in our site. So I though it could be a good idea to use a condition statement. I tried my best to construct a RewriteCond below:
####### START: Mod_rewrite #######
# Enable mod_rewrite
Options +FollowSymLinks
# Start rewrite engine
RewriteEngine on
RewriteCond %{QUERY_STRING} ^action\=([^&]+)\&userID\=([0-9]+)$
RewriteRule ^profile\.php$ /user/%2 [R=301]
RewriteRule ^user/([0-9]+)$ /profile.php?action=view&userID=$1 [L]
####### END: Mod_rewrite #######
but it goes into an infinite loop and results in the URL:
http://www.example.com/user/234?action=view&userID=234
Can anybody lend a helping hand to a newbie please?
Thank you in advance! :=)
TechiePawn
The trick is in the term you used: "Direct client request". The key is determining a direct client request URL from a previously-rewritten URL, and you can do that by testing %{THE_REQUEST} in a RewriteCond.
For more info on implementation, see this thread: [webmasterworld.com...]
Jim
There is a lot of example code in that thread too. What you need is one rewrite and one redirect, each filtered with conditions that stop the infinite loop.
Make sure that you properly take care of www and non-www issues within each of those rules too. You need to avoid a redirection chain.
I'm actually having problems on how to properly use the RewriteRule and RewriteCond statements. I found the following in the Library very helpful:
[webmasterworld.com...] --> Mod_rewrite Beginning
[webmasterworld.com...] --> Mod_Rewrite expressions
Hopefully the 2 posts could enlighten me more on the proper use for mod_rewrite.
Thank! :)
TechiePawn
Thank your for your patience to a newbie. Unfortunately, I can't make it work after several trial and error. Can you enlighten me further where I'm having a mistake please?
Here's my modified .htaccess code:
######### START of .htacccess #####################
# Enable mod_rewrite
Options +FollowSymLinks
# Start rewrite engine
RewriteEngine on
RewriteCond %{REQUEST_URI} /profile\.php\?action=view&userID=([0-9]+)
RewriteRule ^profile\.php\?action=view&userID=[0-9]+$ /user/%1 [L]
######### END of .htacccess #####################
######### START of rewrite log ##################
127.0.0.1 - - [09/Aug/2007:10:46:24 --0600] [www.example.com/sid#8632ed0][rid#90acf58/initial] (3) [per-dir /var/www/html/www.example.com/] strip per-dir prefix: /var/www/html/www.example.com/profile.php -> profile.php
127.0.0.1 - - [09/Aug/2007:10:46:24 --0600] [www.example.com/sid#8632ed0][rid#90acf58/initial] (3) [per-dir /var/www/html/www.example.com/] applying pattern '^profile\.php\?action=view&userID=[0-9]+$' to uri 'profile.php'
127.0.0.1 - - [09/Aug/2007:10:46:24 --0600] [www.example.com/sid#8632ed0][rid#90acf58/initial] (1) [per-dir /var/www/html/www.example.com/] pass through /var/www/html/www.example.com/profile.php
######### END of rewrite log ##################
Thank you in advance :)
TechiePawn
Example:
RewriteCond %{QUERY_STRING} &?action=view&userID=([0-9]+)
RewriteRule ^profile\.php$ /user/%1 [L]