We help IT Professionals succeed at work.

URL Mod Rewriting for Directory Slashes

Last Modified: 2011-09-20
I have a website with user accounts like these: http://www.mywebsite.com/username/

I have this .htaccess file:

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^(.*)/ user.php?uid=$1

It is working fine for urls with the /username/, but when users put the URL in the way http://www.mywebsite.com/username, I get Error 404

I have been trying a lot of RewriteRules, can anyone help me with this?
Watch Question

Just add the following rule:
RewriteRule ^/([a-zA-Z0-9_-]+)$ /$1/ [L]

I don't know what characters you allow in your usernames, so it may need to be tweaked slightly.

RewriteRule ^/?([a-zA-Z0-9_-]+)$ /$1/ [L]

allows letters, numbers, an underscore, and a hyphen
This one is on us!
(Get your first solution completely free - no credit card required)
This one is on us!
(Get your first solution completely free - no credit card required)
JamesCssl:... See.
for the images, css and others files, he will handle this from central configuration varaibles which is also recommended by experts. like:

define("fontEndUrl")  = "http://mysite.com/";
define("images")  = fontEndUrl."images";
define("css")  = fontEndUrl."css";

Now developer will include this configuration file on all PHP pages and safely use url for images as follows:
<img src=<?=images?>raheel.jpg border=0 />
This simple and easy solution for handle such path issues without involment with .htaccess.

Cheers JamesCssl..... ;)


Thanks guys, with all the information I made it work excellent

Thanks again!!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.