I'm in the process of shutting down a site, and have replaced the old site with a single "nobody home" page at the root level of the site. Now I need to set up some redirection, so that any request to any part of the site, no matter how complicated, ends up at the root page.

I've tried what (I thought) ought to work: Creating an .htaccess file containing:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.com/ [L,R=301,NE]

but it mostly fails: Requests to http://www.example.com still get through, but https://www.example.com/doesnotexist.html throws a 404. (If there was no redirection going on, this would be correct, since that page doesn't exist on the site, but that's the point of the redirection: I want this request to be sent to https://www.example.com.)

Arggh. The answer to this is probably obvious to everyone but me; can anyone help out?

PS: I'm in a shared hosting situation, so I have to do this with a .htaccess file rather than hacking a full Apache configuration file.


If you are "shutting down a site" then you probably should not be "redirecting" the old site pages to a single page. An HTTP redirect sends a 301 response code, informing users and search engines the pages have moved. (Although mass redirects to a single page are likely to be seen as soft-404s by Google.)

Instead, you should be serving a custom "410 Gone" response instead. A 410 informs search engines the pages are gone and not coming back. Your "single page" is the custom error document.

For example:

ErrorDocument 410 /single-page.html

RewriteEngine On

# Trigger a 410 Gone for all user requests
RewriteRule ^ - [G]

The additional condition that checks against the REDIRECT_STATUS env var is to ensure that direct requests for /single-page.html itself also trigger a 410 without creating a rewrite loop. The REDIRECT_STATUS env var is empty on the initial request, but set to "410" after the RewriteRule is triggered and the HTTP response status is set.

| improve this answer | |
  • 1
    This looks like the right match for what I'm trying to do (spiders find out the page no longer exists, but humans get a viewable page explaining what's going on). Thanks! – Jim Miller Sep 28 at 15:36

The reason http://www.example.com/ is working and https://www.example.com/doesnotexist.html isn't is because your rewrite condition explicitly disables the rewriting if the client is accessing the site via HTTPS (which I believe several major browsers do by default now, but I don't have a source for this right off the top of my head).

I'm assuming you did that to prevent an infinite loop, which would otherwise infinitely rewrite and redirect every request to (what I assume is) the canonical url, with the client never being able to actually view the page.

I believe the following configuration is what you're looking for. Please note that I tested this on my local server, but I'm running version 2.4.46. As best as I can tell from an admittedly semi-half-assed glance over the version 2.2 mod_rewrite documentation, there weren't any significant changes in the upgrade to version 2.4, at least as it pertains to mod_rewrite specifically.

RewriteEngine On
RewriteCond expr "! %{REQUEST_URI} -strmatch '/'"
RewriteRule (.*) https://www.example.com/ [L,R=301,NE]

This configuration enables the rewrite engine, obviously, after which it checks the URI of the request. If it's not equal to "/", it rewrites the entire thing to only the document root, issues a 301 redirect like you did in your original, and stops processing any further rules. You may or may not need a RewriteBase "/" directive declaration right after the RewriteEngine On. It didn't seem to matter when I tested this, as I was able to rewrite nested-directory URI's with no problem (i.e... /a/b/c successfully redirected to /), but the documentation does make a note to point this out, so here I am passing the warning down.

Again, I did test this, but the server version is different so hopefully you don't run into any problems from that. Please note that in order for this to work (barring technical limitations or differences stemming from the aforementioned version difference), your hosting provider needs to have enabled the AllowOverride directive, probably with the All option, although I wasn't able to pin down exactly what the minimum requirement for the rewrite directives to work was.

| improve this answer | |
  • 1
    You don't actually need a separate RewriteCond directive as the necessary check (that it's not root) can be performed in the RewriteRule itself. eg. RewriteRule . / [R=301,L] - redirects something to root, where something is anything other than root. The NE flag is not required. However, a 3xx redirect may not be the best option here. – MrWhite Sep 27 at 2:32
  • 1
    "there weren't any significant changes in the upgrade to version 2.4, at least as it pertains to mod_rewrite specifically." - actually, you can't use Apache expressions (expr argument on the RewriteCond directive) under Apache 2.2. – MrWhite Sep 27 at 10:35
  • @MrWhite Hey, thanks for the feedback, I wasn't aware of that Rewrite . / shortcut. I actually never used version 2.2 in any real capacity, either, so I really do appreciate these pointers. – Jose Fernando Lopez Fernandez Sep 28 at 14:23

Shouldn't this work?

RewriteEngine On
RewriteRule ^ index.html
| improve this answer | |

Instead of using Apache (or any webserver) to redirect your users to a new website, why don't you do a DNS redirect if you have access to the DNS records for the website?

What is a URL redirect?

URL redirect (URL forwarding) allows you to forward your domain visitors to any URL of your choice (to a new domain or a different website).

You can set 301 (permanent), 302 (unmasked), and masked (URL frame) redirects for the domain names pointed to BasicDNS, PremiumDNS, BackupDNS (a legacy option) or FreeDNS.

If everything is configured properly, it takes about 30 minutes to start working.

How to Redirect a URL - NameCheap.com

Source - NameCheap.com: How to Redirect a URL

| improve this answer | |
  • 4
    Such services almost never support https, and these days, that's a show stopper. – Michael Hampton Sep 28 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.