Welcome to WebmasterWorld Guest from 54.225.16.10

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Rewrite help needed (non-www to www)

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

Junior Member

10+ Year Member

joined:Jan 1, 2004
posts: 64
votes: 0


I want to rewrite: domain.org/map/
to
www.domain.org/map/

I've tried using this example:
[webmasterworld.com...]

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 July 19, 2011 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:14258
votes: 552


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 July 19, 2011 (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
posts:18903
votes: 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 July 19, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 1, 2004
posts: 64
votes: 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 July 19, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 1, 2004
posts: 64
votes: 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 July 19, 2011 (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
posts:18903
votes: 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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members