Home mod_rewrite entire website to front page ONLY of new website
Reply: 1

mod_rewrite entire website to front page ONLY of new website

J Griffiths
1#
J Griffiths Published in 2018-02-13 13:18:57Z

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:

  • Apache redirect to a clean URL
  • https://stackoverflow.com/a/11590814/1738274

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

gargkshitiz
2#
gargkshitiz Reply to 2018-02-14 08:14:07Z

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]
            .......
            .......
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.349403 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO