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

Apache Web Server Forum

Rewrite help needed (non-www to www)

 2:27 am on Jul 19, 2011 (gmt 0)

I want to rewrite: domain.org/map/

I've tried using this example:

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

However this doesn't work. Possibly because I'm adding the directory wrong.

I've tried:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com/map
RewriteRule (.*) http://www.example.com/map/$1 [R=301,L]

My .htaccess file is the /map/ directory. What RewriteBase should I use? And how do I add a directory?



 3:36 am on Jul 19, 2011 (gmt 0)

To clarify: Just this one directory? Not everything else in the domain?

The line
RewriteCond %{HTTP_HOST} .
is not necessary. There is always a host.

Do you have two entirely separate domains, example.com and www.example.com, each with a /map/ directory? This does not seem likely. And if you don't, there is no point to putting the htaccess in the /map/ directory, because requests have either already asked for the correct domain, or they will never see the htaccess.

Is there a reason for redirecting only the requests for this directory? If not, you can save yourself a lot of hair-pulling by looking at your host's fine print. They may already have an option for directing everything to either www. or not-www. if you click the right button. No reason to mess with htaccess if you don't have to.


 8:02 am on Jul 19, 2011 (gmt 0)

There is always a host.

There isn't if it is a HTTP/1.0 request.

That's why the oft-quoted
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ pattern is used (and it parses faster than two separate conditions).

Additionally, with no end tag on the pattern, the rule would fail to redirect requests for www.example.com:80 to the canonical URL.

HTTP_HOST can match ONLY the requested host name and server port. It cannot see protocol, path or query string data.

 4:58 pm on Jul 19, 2011 (gmt 0)

Lucy - the person is in the right directory. I'm redirecting non-www to www for that directory.

g1smd - how do I get that pattern to work with a directory?


 5:34 pm on Jul 19, 2011 (gmt 0)

I got it to work with a php rewrite.

If "www" isn't in the HTTP_HOST, I have javascript change the url using window.location.href to [domain.org...] + $_SERVER['PHP_SELF']+$_SERVER['QUERY_STRING']


 11:01 pm on Jul 19, 2011 (gmt 0)

Your javascript rule will not issue the 301 redirect the searchengines need to see. You MUST use the HEADER function in PHP or else a RewriteRule in the .htaccess file.

The RewriteRule pattern matches the path part of the URL as long as you use the correct pattern for the Regular Expression.

You only need a preceding RewriteCond when you want to look at SERVER_PORT, HTTP_HOST, QUERY_STRING, and many other things that are not the PATH part of the URL request.

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