How do I Solve this Mod Rewrite Problem?

Hi, I want all www. subdomain requests to trigger a 404 error.
I have gotten this to work for all pages in the root directory,
but for pages in subdirectories it doesn’t work.

Here’s the .htaccess code:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\\.domain\\.com(/(.*)/)*$ [NC]
RewriteRule ^$ [R=404]

So triggers a 404 no problem, but doesn’t trigger anything…

I have tried for a few hours to make this work with lots of experimenting but I can’t figure it out…

Can anybody help?

  1. The %{HTTP_HOST} variable is just that: it contains the host, not the requested subdirectory. So you can leave out the part in there matching directories; that’ll never happen.

  2. The ^$ in RewriteRule is causing only / to be 404 redirected. Instead of ^$, use .? – meaning the requested URL starts with any character, or none at all.

BTW. Why do you want to 404 redirect? Why not 301 redirect to the real location you want people to go?

Thanks, I have it like this now:

ErrorDocument 404 /errors/404.php

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\\. [NC]
RewriteRule .? [R=404]

The problem though is the custom error page… When I set it I get this error:

The requested URL /[R=404] was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Without the custom page it works fine, but I need the custom page.
When I used ^$ previously the custom error page worked fine, that’s why I used it.

Do you see a solution for this?

Hostgator is my host in case that matters.

And I want to throw a 404 because of google content removal guidelines (I want only indexed, no www).


The RewriteRule syntax is

RewriteRule {regex} {redirection} [{optional flags}]

Because you have NO redirection, I’m actually rather shocked that you’re not getting 500 errors from Apache.

If you want ALL www’d requests to trigger a 404, ScallioXTX was correct, you can’t specify an EMPTY {REQUEST_URI} string, you must specify something which will match ALL {REQUEST_URI} strings. To me, .? is the simplest as it will match your nothing and it will also match one of ANY character so that suits your situation far better than the empty string.



You should indicate that you don’t want to redirect anywhere, i.e. add the dash I indicated in red below.

ErrorDocument 404 /errors/404.php

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\\. [NC]
RewriteRule .? [COLOR="Red"]-[/COLOR] [R=404]

If you only want the non-www domain indexed, why not 301 redirect all www-request to non-www requests. That way google will give any juice that the www currently has to the non-www versions, instead of completely destroying it which a 404 will do.

Hi, when I use RewriteRule .? - [R=404] I get a 500 error, but there’s another way. Here’s my experience with a 301:

I have another site where I also wanted to remove the www.

After I added…

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\\$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

After I added that the new pages were indexed without the www, but the main domain which was already indexed remained and remains indexed with the www (there is no non www version indexed of that main domain still).

So that’s why I want to throw a 404 now. To get google to listen to me and remove those indexed www subdomain pages. I want them replaced with the with the non www versions (after that’s done I’ll change back the 301).

But since RewriteRule .? - [R=404] throws a 500 error (might be hostgator specific issue because this only happens when I specify ErrorDocument 404 /errors/404.php)… But since that is the case, what I’m now trying to do is…

Just throwing 404’s for the 2 pages currently indexed, which are:


That should take care of the problem as well if it is possible to do that.

Still experimenting with that though, not having success so far…

RewriteCond %{HTTP_HOST} ^www\\$ [NC]

You did correctly escape the first dot, but you need to escape the second dot as well.

RewriteRule ^(.*)$$1 [R=301,L]

That’ll work, but the following is more efficient:

RewriteRule .?{REQUEST_URI} [R=301,L]

You don’t expect google to change the results instantly after you’ve changed the .htaccess do you? That’ll take a few days up until a few weeks, dependent on how often google crawls your website.

I’d leave it at the 301 and let things run their course. If you use a 404 you will maybe achieve the effect that google removes the pages from their index eventually, but in the meantime anyone that clicks such a link will get a “not found”, go away and never come back. Whereas if you redirect they will see your webpage just fine.

“You don’t expect google to change the results instantly after you’ve changed the .htaccess do you? That’ll take a few days up until a few weeks”

I understand but for the the other site the 301 has been set over a year ago, maybe it’s not updated because the site has been barely updated since then.

Anyway, I just have set all www.'s to 404. No custom error page since I had to remove the ErrorDocument 404 /errors/404.php for it to work but I’ll see how it goes. The minute the www.'s are gone I’ll make hem 301’s. There’s no traffic going to the www.'s atm so that’s not an issue, the site is still brand new.

Thanks for your help.