homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Dynamic to Pseudo-Directory with .htaccess (Help!)
Changing dynamic URL to pseudo-directories with .htaccess

WebmasterWorld Senior Member 10+ Year Member

Msg#: 4406857 posted 6:59 pm on Jan 14, 2012 (gmt 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.)




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

Msg#: 4406857 posted 7:41 pm on Jan 14, 2012 (gmt 0)

If you need it to display in the browser like
http://www.example.com/profile/myuserid 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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved