Forum Moderators: phranque
but now i find google has indexed an entire copy of the site because someone linked with an error in their code to give the url of %20www.mydomain.com
Any ideas how to change the htaccess to 301 any url that isnt www to the correct www?
If it still doesn't work, then investigate whether you (or your host) have any other code in httpd.conf or .htaccess files that will interfere with it. The code will work fine as long as:
A) Requests for the malformed/incorrect domain are resolved to the directory in which this .htaccess file resides.
(That is, wild-card DNS and server name configurations must be correct)
B) The .htaccess file is processed mod_rewrite code is invoked for those domain requests.
C) No other code is invoked which interferes with this code.
Jim
In order for the code to have any effect, the server must deliver the request to the directory where the code resides. Since it works for non-www, that indicates that you've probably installed it correctly and that it is being invoked for the non-www requests. But it sounds like it's not being invoked for malformed %20www requests.
Also, when posting here that 'it doesn't work' it's often helpful to post details about the error: What happened? How did that differ from what you expected? What did you see in the browser? What did you see the the server access log and the server error log? If you got a 404, what was the URL in the address bar? Posting details like these can help others help you, and can be a good investment of your time.
Jim
When i use the code
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com
RewriteRule (.*) [mydomain.com...] [R=301,L]
domain.com redirects to www.domain.com just fine but anyhting else in the broswer i.e ww or www1 or %20www returns the full server prefix and does not rewrite to www. If i try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^%20www.domain.com
RewriteRule (.*) [domain.com...] [R=301,L]
the code has no affect at all. If i try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ www.domain.com
RewriteRule (.*) [domain.com...] [R=301,L]
with the gap instead of %20 it generates a 500 error for all urls of all prefixes. It doesnt matter if i 404 or 301 %20www but cant get it to do either. The hosts own website also displays the %20www if you enter the url that way instead of of throwing up a 404. I
RewriteEngine On
RewriteCond %{HTTP_HOST} ^\ www.domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
So, it's back to your hosting provider for help with the httpd.conf setup, or on to a different host. Tell them you want a ServerAlias of
ServerAlias *.domain.com
Jim
How might wildcard DNS apply, or not apply, to the situation soapystar is posting about?
Also, is ServerAlias *.domain.com
a) a substitute for wildcard DNS?... or
b) a module that must be enabled for your code to work to rewrite wildcard DNS?
c) something else entirely?
ServerAlias *.domain.com
Is this generally a default on most servers?
I ask because I've never known about ServerAlias before (there's much I don't know about with Apache servers ;) ) and want to find out what bases need to be covered when I set up redirect to www.
So far, in my ignorance, I haven't run into any problems redirecting wildcard dns, but I'm wondering if I've just lucked out.
ServerAlias *.domain.comIs this generally a default on most servers?
I'd guess that the default on commercial name-based virtual hosting is
ServerAlias www.domain.com
That is, defining only the www subdomain as an alias for the main domain.
I'd like to see more experimental results like the server headers check results. Technically, a leading space or encoded space violates the HTTP/1.x protocol, and I was surprised that it worked at all.
Jim
With space before www (after http), my browser (IE6) displays %20www in the address window, and I get a 404 instead of the page. Again, this site doesn't have the rewrite. With sites with the rewrite, when I see the %20www, I don't get the 404.
So, the problem seems to be "enabled" because of the wildcard DNS and the rewrite to www. This may already be obvious to Jim and soapystar, but I wasn't sure what would happen, so I tried it and am reporting.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^\%20www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
I tried the spaced-www test myself on a site *without* wild-card DNS, and got a DNS lookup failure.
Jim
[edit] Examplified domain [/edit]
[edited by: jdMorgan at 4:09 pm (utc) on Sep. 29, 2005]
RewriteCond %{HTTP_REFERER} ^(.*)domain.com(.*)$
RewriteRule (.*) [domain.com...] [R=301,L]
have one line added to say EXCEPT www.domain.com?
I think ^(.*)domain.com(.*)$ works as a redirect for the %20www but then you get into a loop with it redirecting to the www which is then redirected to itself giving the EXCEEDED MAXIMUM REDIRECTS error.
Thanks.
in the end the fix was to remove the wildcard from the dns.....
soapystar - This is pretty much the decision I'd come to as well. I tested a bunch of existing domains I'm involved with that had rewrites to www, and the only ones that gave me problems with the space were those with wildcard DNS.
What I haven't tried yet is to create an extra A-record for w.domain.com and for ww.domain.com, as well as www and domain.com. These should all be rewritten to www with the existing code, and would essentially do much the same thing as the wildcard DNS. If you try it before I do, please post a note about it.
PS: To correct one misstatement in my post above about this, with the space in the urls without the wildcard DNS, I was getting a "server not found" message, not a 404.