1

Now I've written dozens of redirects in my time, some with tricky regex, some more tame, but today, the very simplest redirect is stumping me on a CentOS server, running Apache 2.2.3.

All I'd like to do is redirect every single request on an old domain, regardless of path and query string, to the front page only of a new site. This is why, for example, a mod_alias Redirect directive isn't appropriate, since it appends the path to the new address.

In an Apache conf file, where the virtual server is defined, I now have

<VirtualHost THE.IP.ADDRESS:80>
        DocumentRoot "/var/www/html/SITE_ROOT"

        ServerName OLD_DOMAIN.com

        <Directory "/var/www/html/SITE_ROOT">

                Options  FollowSymLinks
                RewriteEngine On
                RewriteRule ^$ https://NEW_DOMAIN [R=301,L]

                AllowOverride None
        </Directory>
</VirtualHost>

While the redirect to https://NEW_DOMAIN occurs as expected, the path of the original request is always appended, leading to 404 errors on the new site.

For example, visiting http://OLD_DOMAIN.com/asdf

redirects to https://NEW_DOMAIN.com/asdf

...when I'd actually want to arrive at https://NEW_DOMAIN.com/

Why is the path being appended, even though I'm not collecting a pattern match, and am not specifying such a match in the destination?

There are plenty of answers like this on SO already:

But I can't find a discrepancy comparing these solutions against my own configuration. Any ideas?

J Griffiths
  • 701
  • 6
  • 14
  • The only difference I spot is the missing trailing / in the `RewriteRule ^$ https://NEW_DOMAIN`. Maybe you can try with `RewriteRule ^$ https://NEW_DOMAIN/` – Simonluca Landi Feb 13 '18 at 13:32
  • I'm afraid I'd already tried that, but sadly to no effect... Thank you though! – J Griffiths Feb 13 '18 at 13:44
  • Have you cleared browser history, just to make sure you’re not tricked by the browser cache? (Some browsers cache redirects really tenaciously, so if you tested this before with a different htaccess setup which did intentionally include the path, this might be the cause.) – CBroe Feb 13 '18 at 13:55
  • Thanks, but I've tried different browsers and even entirely different devices- and at no point did I have it configured to include the path. While this tenacious caching has often been the source of my problems in the past, this time for once, it is not. Thanks though! – J Griffiths Feb 13 '18 at 14:15

1 Answers1

0

RewriteRule ^(.*)$ https://NEW_DOMAIN [R=301,NC,L] should work. I have tested with various URLs and it always redirects to https://NEW_DOMAIN

My config structure looks a bit different:

<VirtualHost *:80>
    ServerName OLD_DOMAIN.com
    DocumentRoot "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs"
         <IfModule mod_rewrite.c>
            RewriteEngine on
            Options  FollowSymLinks
            RewriteRule ^(.*)$ https://NEW_DOMAIN [R=301,NC,L]
            .......
            .......
gargkshitiz
  • 2,130
  • 17
  • 19