Forum Moderators: phranque

Message Too Old, No Replies

Help in mod rewriting the long/cryptic URL to a readable one

assistance in transforming ugly url to a readable/manageable one

         

TechiePawn

5:47 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



Good day Everyone!

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

and1c

7:14 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



Try this

RewriteRule ^user/([^/]*)-.html /profile.php?action=view&userID=$1 [l]

and1c

7:20 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



Sorry...the one above is not correct as it will hyphenate a URl when rewritten!

I will correct and repost in a minute

jdMorgan

8:07 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code explicitly creates an infinite loop, because REQUEST_URI and the internal Request_Rec "seen" by RewriteRule are both updated on each rewrite. So you're redirecting A to B, and then rewriting B right back to A.

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

g1smd

12:30 am on Aug 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There was a post a couple of weeks ago where someone asked almost the exact same question.

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.

TechiePawn

10:09 am on Aug 9, 2007 (gmt 0)

10+ Year Member



Thank you very much for your help guys.

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

g1smd

4:12 pm on Aug 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Post up your code here when you have it working.

TechiePawn

5:12 pm on Aug 9, 2007 (gmt 0)

10+ Year Member



Hi g1smd, JDMorgan and the rest of the gang,

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

jdMorgan

5:17 pm on Aug 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Query strings are not 'visible' to RewriteRule, and are not part of %{REQUEST_URI}.
You will need to use a separate RewriteCond examining %{QUERY_STRING} if you wish to match or back-reference query strings.

Example:


RewriteCond %{QUERY_STRING} &?action=view&userID=([0-9]+)
RewriteRule ^profile\.php$ /user/%1 [L]

Jim