Wildcard redirect in .htaccess - having trouble

I’m having some trouble with this. I’ve set wildcard redirect in cpanel, but it’s not doing what I want. What I need is to have Example.com/AnyNonExistentURLString.html or anynonexistentsubdomain.Example.com to go to www.Example.com, except for Example.com/thisoneonly.htm and Example.com/thisotheroneonly.htm. All I get instead is that nonexistent urls take visitors to an error page served by the hosting provider.

So far I haven’t found anything that solves the problem. Also, I would like to be able to do this myself in .htaccess only and not have to be concerned about httpd.conf or anything like that now. Thanks if anyone knows how to do what I’m trying to do here…

So in a nutshell, this is what I have in .htaccess now:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^/?$ “http\:\/\/www\.example\.com\/” [R=301,L]

All that does is send example.com to www.example.com.

What I need most of all beyond that is for (with or without www .)example.com/AnythingHere.htm to go to www.example.com. I’m much less concerned about any subdomains.


Moved to Apache forum (home for .htaccess questions).

Okay, redirect any non-existant file to the DirectoryIndex at www.example.com EXCEPT casea and caseb. NO subdomain will not be received/handled unless Dynamic Mass Virtual Hosting is used in which case it’ll redirect to the subdirectory which can be handled by this code, too.

# .htaccess in DocumentRoot

# If file does not exist
RewriteCond %{REQUEST_URI} !-f

# AND if directory does not exist
RewriteCond %{REQUEST_URI} !-d

# AND if not casea (repeat for caseb)
RewriteCond %{REQUEST_URI} ^/?path/to/casea\\.ext$

# Redirect to ...
RewriteRule .? http://www.example.com [R=301,L]

For more information about mod_rewrite, check the tutorial linked in my signature (from years in the Apache forum).



Thanks a lot, David. I appreciate anything that can get me better acquainted with this business of coding in .htaccess, and your post and links definitely merit further study. Even though I couldn’t find anything on the Web or in the forum for my host, it occurred to me that perhaps I should just put in a ticket and ask (imagine that). So a rep quickly responded with an answer that has also been working excellently for example.com/any-nonexistent-url-string.htm. Here’s the code:

ErrorDocument 404 http://www.example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

This nicely forces non-www to www as well as taking care of bad url string data appearing after the “.com/” by sending nonexistent urls to the main page. Valid “*.com/anything.htm” urls work properly and go to www too, and I like how clean and succinct this solution is. The subdomain part is another story of course. Here’s a summary of what he said was needed to fix that in the event of nonexistent subdomains needing to be redirected:

  1. add a subdomain through cpanel, name it * and point it to the public_html directory.

  2. You need to create a * DNS record as well.

  3. Each subdomain has it’s own VirtualHost block under Apache, so existing ones would be directed where they need to go.

  4. Non-existing subdomains would be sent to the main domain and be caught by the .htaccess file. You could then change the line “RewriteCond %{HTTP_HOST} ^example.com [NC]” to read “RewriteCond %{HTTP_HOST} !^www.example.com [NC]” instead.

By the way, and please pardon my ignorance, but what exactly do you call this kind of coding or programming in the .htaccess file, and what do you call this language that’s being used?

I have to admit, I was hoping to be able to do it all myself in .htaccess, and that subdomain stuff definitely requires further study. Any further thoughts you have on this would of course be great, David. I was most concerned about the first part which can be taken care of in .htaccess only, and it works well, but I also want to look further into your post about that part, unless of course the code I posted above winds up achieving all the same ends. I’m very happy there was a .htaccess-only solution to the first part, though.


Silly me! Of course ErrorDocument is the preferred way to go! One of the things to learn with mod_rewrite is when NOT to use it!

Of the other, that mod_rewrite is specifically for Apache 2.x and could be improved upon to

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\\.example\\.com$ [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI} [L,R=301]

The differences here are:

  1. That the {HTTP_HOST} variable is matched against the desired domain name and, if not matched

  2. Will redirect anything to the www’d domain with the original {REQUEST_URI} string. The R=301 makes it a permanent redirection and the Last flag closes this block statement.

Bad url string data? If you mean {REQUEST_URI} strings, that’s handled by the ErrorDocument statement (part of the Apache core). The !-f and !-d do the same thing for files and directories (when examining the {REQUEST_URI}) but that a poor use of mod_rewrite - unless you need to so something else with it (e.g., redirect to a script which handles all requests like with WordPress - ErrorDocument would KILL a WordPress install!).

Subdomain pointed at the domain’s DocumentRoot? Horrors! Even cPanel should have a Mass Dynamic Virtual Hosting setup for subdomains (which would handle the DNS record for you)!

  1. Non-existing subdomains would be sent to the main domain and be caught by the .htaccess file. You could then change the line “RewriteCond %{HTTP_HOST} ^example.com [NC]” to read “RewriteCond %{HTTP_HOST} ![1](http://www.example.com) [NC]” instead.

I thought I said that already!

This kind of coding is mod_rewrite.

I’m not not fond of subdomains so I have no experience with mass dynamic virtual hosting. Perhaps the modVPS folks (Tony or Sari or ???) can help here?



  1. www.example.com ↩︎