Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite, username

use of username with mod_rewrite

         

bsakkers

1:55 pm on May 26, 2010 (gmt 0)

10+ Year Member



Hello there,

i am building this site where a user can use www.domain.com/username to see his/her personal page.

i have used (i am very new to this!) :

RewriteEngine on
rewritecond %{request_uri} !^/images
rewritecond %{request_uri} !^/javascripts
rewritecond %{request_uri} !^/stylesheets
RewriteRule ^([A-Za-z0-9-]+)$ card.php [L]
RewriteRule ^$ card.php [NC,L]

so the url www.domain.com/index.php wont be redirected.
But I want the user to be able to type www.domain.com instead of www.domain.com/index.php
When i now fill in www.domain.com the user is redirected to card.php ...

any ideas?

Kind regards,

Barry

jdMorgan

3:01 pm on May 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your second rule clearly rewrites all requests for your "home page" to /card.php. Delete it or comment it out as shown.

You can combine your RewriteConds as shown for better efficiency in .htaccess.

Use [NC] to make the RewriteRule pattern case-insensitive and eliminate the need to check for both "a-z" and "A-Z". This will speed up processing by about 40%.

Should you wish to use your second rule after modifying it in some way, note that there is no need to use [NC] if the pattern does not specify any alphabetic characters.

I recommend that you use the exact directive casing and syntax shown in the mod_rewrite documentation at apache.org for readability and in order to allow for maximum 'portability' of this code across servers.

RewriteEngine on
#
# Exclude URLs for shared-object directories to allow that sharing and to
# prevent 'collisions' between shared-object directory names and usernames
RewriteCond $1 !^(images|javascripts|stylesheets)
# Internally rewrite requests for extensionless URLs in top-level directory to card.php
RewriteRule ^([a-z0-9\-]+)$ card.php [NC,L]
#
# Internally rewrite main "home page" requests to card.php
#RewriteRule ^$ card.php [L]

Note that the RewriteCond is only needed to prevent the rule from being invoked if one of the shared directory names is entered without a trailing slash. If entered with a trailing slash or with a slash plus additional path-info, the RewriteRule pattern won't match, so the RewriteCond won't even need to be evaluated. This is just a note to explain how it works -- the code is fine.

Comment your code. This really helps remind you of its purpose and intent when you go back to it later, and certainly helps when posting questions in forums... :)

Jim