Help - problem with www.domain.com => domain.com

Hi All,

I thought this was pretty straightforward, but apparently I got something real wrong.

The rewrite below is supposed to redirect all www prefixed urls to zvis.com domain, including and preserving subdomains.

RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?zvis\.com)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L,QSA]

Now, somehow the only timethisrule works is when I access www.zvis.com

if there is anything after .com, i.e any other file or directory it doesn’t fire.
E.g. http://www.zvis.com/dep/dep.shtml never gets redirected to http://zvis.com/dep/dep.shtml
I’ve checked HOST parameter in the GET query headers, it’s www.zvis.com, so why the rule isn’t working for anything other than [URL=“http://www.zvis.com?”]www.zvis.com?

Hi Zvi!

mod_rewrite IS very straightforward and what you got wrong is that your code is ONLY designed to strip the www from the URI. Do you have both the non-www and the www version allowed by your host (because your code should work as you expect)?

RewriteCond %{HTTP_HOST} ^www\\.(([a-z0-9_]+\\.)?zvis\\.com)$ [NC]
# RewriteRule ^(.*)$ http://%1/$1 [R=301,L,QSA]
# the following will do the job just as well and is easier to understand
# QSA is automatic UNLESS you're creating a new query string
RewriteRule .? http://%1/%{REQUEST_URI} [R=301,L]

You should be able to tell about the host if you can see the DNS records - otherwise, ask them to enable the non-www version of your domain.

Regards,

DK

Thanks Dave!

[quote=dklynn;4489339Do you have both the non-www and the www version allowed by your host

You should be able to tell about the host if you can see the DNS records - otherwise, ask them to enable the non-www version of your domain.
[/quote]
I’m maintaining dns records myself. So, I guess I am missing something but what? in dns I can see records for @ and www as A type… Anything else I should have?

Zvi,

In my DNS record, I have my domain name, 14400, IN, A and my IP address. Following that, I have www, 14400, IN, CNAME, my domain name. Those are the relevant settings in my WHM DNS panel. If you’re using your own VirtualHost file, include the non-www’d domain name in the declaration.

Regards,

DK

I do have both in virtual host, i.e.zvis.com as a server name and www.zvis.com as serveralias.

In dns records though I don’t have anything like 14400 or IN?
Just @ with ip and www with ip…

Zvi,

Bummer! What about your hosts file? Does it accept zvis.com?

Regards,

DK

Well, I sortta found a reason, but I don’t understand why.
In the child directory which refuses to redirest www to non www there is another .htaccess.
In that htaccess I have other local redirect rules. But they’re irrelevant since,
even if I put just these 2 lines in the child dir .htaccess
RewriteEngine on

RewriteEngine Off

www doesn’t get redirected anymore… I thought top level was processed first?

Zvi,

The top level IS processed first so you are correct. By the time the subdirectory’s .htaccess is parsed, the redirection should already have been made.

Regards,

DK

That’s the weird part, in theory lower level .htaccess should have no effect but it does…