Forum Moderators: phranque

Message Too Old, No Replies

Redirect directory to php query

         

RobsterUK

6:52 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



Hi

I'm having a bit of a problem with mod_rewrite. I've read the manual and various tutorial sites, and just can't get it working.

All i want to do is do a redirection like this:
www.domain.com/username >> www.domain.com/script.php?user=username

If someone could tell me how to do this i'd be grateful.

jdMorgan

7:12 pm on Jan 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RobsterUK,

Welcome to WebmasterWorld!

Please post your code so we can see where you're going wrong.

Jim

RobsterUK

8:12 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



The code i'm using at the moment is below.

rewriteEngine on
rewriteRule ^/([^yse]*)$ /yse/index.php?action=profile;user=$1 [L]

The folder of the script is /yse so i'm trying to say any request that isn't that will put the request onto the end of the php query.

Requests for /yse are loading as normal, but if you try anything else including a valid username you just get a 404

jdMorgan

8:27 pm on Jan 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The pattern "[^yse]*" means, "any number (including zero) of any characters except for y, s, or e." It does not mean "any string not equal to "yse". See the regular-expressions tutorial cited in our forum charter for more info. Try something like this instead:

RewriteEngine on
RewriteCond %{REQUEST_URI} !/yse
RewriteRule ^/(.*)$ /yse/index.php?action=profile;user=$1 [L]

You may also need to exclude requests for robots.txt and image files from being rewritten.

Jim

RobsterUK

10:09 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



Thanks for that.

This is my completed code as it stands, seems to work fine.

rewriteEngine on
RewriteCond %{REQUEST_URI}!/yse
RewriteRule ^/([a-z0-9]*)$ /$1/ [R]
RewriteRule ^/(.*)/$ /yse/index.php?action=profile;user=$1 [L]

First rule adding a slash, and at the same time having the effect of making images and other files in the site root continue to work.
Second rule assuming there is a slash and rewriting

jdMorgan

11:04 pm on Jan 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why are you using a 302-Found external redirect for your first rule?

Jim

RobsterUK

11:35 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



Its just there while i'm testing it because I had some problems with that rule and i wanted a way of easily seeing the resulting URL