Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite not working

         

kb1100

1:32 am on Sep 19, 2007 (gmt 0)

10+ Year Member



I have been reading this section of the forum to try to figure out how to rewrite domains like this: http://www.example.com/user.php?user=USERNAME to http://www.example.com/USERNAME

I would like to get the urls to change to something like this for the benefit of my users. This is what I came up with:

RewriteEngine on
RewriteBase /
RewriteRule ^user.php?user=(.*)$ /$1 [R]

However its not working and I really have no clue what I am missing. Any help would be greatly appreciated.

[edited by: jdMorgan at 1:58 am (utc) on Sep. 19, 2007]
[edit reason] example.com [/edit]

jdMorgan

1:59 am on Sep 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The short answer is that you're going about it backwards.

If you want the URLs to change, you need to change your on-page links, which define the URLs on the Web, to the /username format.

Having done so, you then rewrite those /username URLs, when requested from your server, back to the /user.php?user=username format needed to invoke your script. mod_rewrite works after a request is received by your server, but before any content is served or any scripts are invoked. It cannot modify the pages output by your server.

More detail here [webmasterworld.com].

Jim

kb1100

2:19 am on Sep 19, 2007 (gmt 0)

10+ Year Member



Okay.

Im still not sure what I need to do though. When you say "change your on-page links" do you just mean that I should goto the url http://www.example.com/username instead of looking for a redirect from the old url? Or do you mean that I need to change the user.php file that I am trying to change?

jdMorgan

11:08 pm on Sep 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to change whatever it is that produces the pages seen by your visitors -- whether they're hand-coded HTML or produced by a script. Change the links on those pages to the form you want to use -- and that you want search engines to see. Then add code to your server config to recognize those new URLs and 'steer' requests for them to the correct 'internal' URL-paths -- the URL-paths you used to use.

Having done that, you can take a third optional step -- redirect any direct requests for the old URLs to the corresponding new ones. But this step is tricky, optional, and not a part of the required solution.

Jim

g1smd

11:30 pm on Sep 19, 2007 (gmt 0)

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



Link to www.domain.com/blog/blah/ on your pages and then use a rewrite to tranlate that URL request into the /script.php?param=blog&param=blah internal server filepath that will get the content.

Additionally, you will need an external redirect such that if someone requests the "dynamic" URL, the server does NOT serve this Duplicate Content, but instead forces the browser to make a new request for the correct URL.

Some clues are in: [webmasterworld.com...]

kb1100

2:13 am on Sep 20, 2007 (gmt 0)

10+ Year Member



Thank you guys both so much for your help!

g1smd Your explanation really cleared things up for me, and the thread you linked to was a great help.

I have one question now that its working though. I used the following to rewrite:

RewriteRule ^([^/\.]+)/$ /user.php?user=$1

That seems to work perfect, although I know seeing I have "([^/\.]+)/" that the content will only display when it is followed by a /. So my question is seeing that the ([^/\.]+) is referring to a user name, which there will be many of. Should I also add a rule which will cover this in the case of a type in for instance where the user does not add the "/"? Will it matter if I add the second rule?

Thanks again!

jdMorgan

2:24 am on Sep 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To make the trailing slash optional, just follow it with the regular-expressions "?" quantifier:

RewriteRule ^([^/.]+)/?$ /user.php?user=$1 [L]

And if these are usernames, you might want to be more specific about what characters the rule accepts:

RewriteRule ^([a-z0-9_\-]+)/?$ /user.php?user=$1 [NC,L]

Jim

g1smd

11:19 am on Sep 20, 2007 (gmt 0)

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



If you make the trailing slah optional, that will allow the site to serve Duplicate Content. Each "page" of information would have two URLs that can be used to access it. Personally, I would standardise on always having a trailing "/".

g1smd

1:33 pm on Sep 20, 2007 (gmt 0)

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



slah --> slash

jdMorgan

3:39 pm on Sep 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good point. So the solution is to force either slash or non-slash. For example, you could force a slash with:

RewriteRule ^([^/.]+)$ http://www.example.com/$1/ [R=301,L]

... (any www- non-www domain redirects here) ...

RewriteRule ^([^/.]+)/$ /user.php?user=$1 [L]

Jim