homepage Welcome to WebmasterWorld Guest from 54.234.0.85
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

    
redirect www to non www
jackstonehouse



 
Msg#: 4420790 posted 9:00 am on Feb 23, 2012 (gmt 0)

Hi everyone.

Am have trouble sorting out a redirect.

Basically I have added:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.co.uk$
RewriteRule (.*) http://example.co.uk/$1 [R=301,L]


to my .htaccess but the redirect isnt working properly.

It redirects www.example.com/pagename back to example.com when it should be example.com.pagename.

Thanks in advance

Jack

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4420790 posted 9:50 am on Feb 23, 2012 (gmt 0)

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

RewriteCond as written: If the requested domain is anything other than exactly "example.co.uk" (whoops! forgot to escape a \.there) ... then

RewriteRule as written: Redirect to http://example.co.uk keeping the rest of the URL unchanged.

It redirects www.example.com/pagename back to example.com when it should be example.com.pagename

Do you mean that no matter what file you request, you end up on the front page instead of on the page you asked for? There's got to be another rule making this happen. Your rule has the capture () and reuse $1 in the right places, so you have to look elsewhere.

Please say that ".pagename" is a typo for "/pagename" and ".com" is a typo for ".co.uk". Otherwise none of it makes sense.

The rule you quoted should come at the very end of all your redirects, after the more specific redirects. (Does not apply if, ahem, you don't have any other redirects.) Putting it earlier probably won't cause errors, but it may lead to a single request being redirected twice. And one of those later redirects is the one causing the problem.

lappert2001

5+ Year Member



 
Msg#: 4420790 posted 10:19 am on Feb 23, 2012 (gmt 0)

If it's just www to non-www (without the complexities of pagename), most domain registrars allow you to set up A records for example.com, www.example.com and *.example.com. Or you can do a URL redirect at the registrar level as well.

You can also use ServerAlias to do that:
ServerName example.com
ServerAlias *.example.com

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4420790 posted 5:35 pm on Feb 23, 2012 (gmt 0)

An alias doesn't cut it. Requesting one should 301 redirect to the other. This is to avoid both versions returning "200 OK" status as that's a Duplicate Content scenario.

Use the Live HTTP Headers extension for Firefox to investigate the server responses. You're perhaps looking for a double redirect.

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