Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Dynamic to Pseudo-Directory with .htaccess (Help!)

Changing dynamic URL to pseudo-directories with .htaccess

6:59 pm on Jan 14, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 31, 2003
votes: 0


I am trying to set up a redirect on my site and not having much luck. I can "hard code" it like below:

Redirect 301 /profile/myuserid http://www.domain.dom/profile.php?user-id=myuserid

And that works. But, I would like it to:

a) display in the browser as "http://www.domain.dom/profile/myuserid" instead of "http://www.domain.dom/profile.php?user-id=myuserid"

b) work using variables instead of hard coding each actual user ID into the .htaccess file.

Additionally, if there is an actual directory at /profile/myuserid, I would like that to be displayed instead of redirecting/rewriting the dynamic URL.

Hopefully, that all makes sense.

I've searched different places online and tried numerous variations but nothing has worked so far. Some have blocked the whole website from working while others seem to do nothing at all.

The most recent is this:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/profile/([^/]+) http://www.domain.dom/profile.php?user-id=$1 [NC]

I'm not sure where to go next. I probably spent a good afternoon trying to get it to work, which ended with me eMailing my host to see if mod_rewrite was set up on the server. (It is.)

7:41 pm on Jan 14, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
votes: 0

If you need it to display in the browser like
then that is the URL you need to link to from the pages of your site.

URLs are defined in links.

Once the request arrives at your server you employ a rewrite using a RewriteRule to fetch the content from the dynamic internal filepath where the content really resides.

A RewriteRule with a domain name in the target delivers a 302 redirect. You need an internal rewrite. Edit your exisitng RewriteRule code to remove the hostname from the target. Add the [L] flag too.

You will also need a redirect for agents requesting the URL in a parameterised format. Use another RewriteRule for this. This prevents duplicate content. This rule will also need to test THE_REQUEST with a preceding RewriteCond. Without this there will be an infinite redirect-rewrite loop.

This redirect + rewrite question is asked many times each month and there are literally thousands of previous threads with example code.