Forum Moderators: phranque

Message Too Old, No Replies

Another mod rewrite questions

         

formasfunction

3:17 pm on Sep 17, 2007 (gmt 0)

10+ Year Member



I'm trying to script a mod_rewrite to do the following:

all requests for:
example.com/username
go to:
example.com/userinfo.php?user=username

and all requests for:
example.com/username/comments/
go to:
example.com/comments.php?user=username

The first one I managed to get working with the following rules:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^([^/\.]+)/?$ userinfo.php?user=$1[L]

But I'm not sure how to get the second one to work with it.

formasfunction

3:55 pm on Sep 17, 2007 (gmt 0)

10+ Year Member



I think I have it:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^([^/\.]+)/comments/?$ comments.php?user=$1 [L]
RewriteRule ^([^/\.]+)/?$ userinfo.php?user=$1 [L]

g1smd

11:36 pm on Sep 17, 2007 (gmt 0)

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



Along with the rewrite from "static" format to "dynamic" format, you also need a 301 redirect from the "dynamic" format to the "static" format so that none of the "dynamic" URLs can ever be indexed.

Don't forget to sort out www and non-www domain canonicalisation within that same redirect rule, to avoid a redirection chain occuring with any of the other redirects on the site.

jdMorgan

11:42 pm on Sep 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could save a lot of wasted CPU time and lots of confusion over valid versus invalid usernames, by putting all the username-dependent virtual files in a /users/ subdirectory (or some similar name). As it stands, a user named "Mr. Stats" would not be able to access your site if you have a /stats/ directory...

By preceding these URL-paths with /users/ (or something similar), you can greatly reduce the chances for 'collisions' between usernames and existing filenames, and also eliminate the need to check you filesystem to see if the requested URL exists as a file or directory for every single request made to your server. Doing so now will prevent the need for a re-design (or a server upgrade) in the future if your site gets busy.

Jim