0

I need every page request on a site -- 200, 404, etc. -- to land at a fixed page on the site, in this case index_new.php.

I've tried other SO and ServerFault answers and I continue to get an endless redirect loop.

Here's my code in httpd.conf:

<VirtualHost *:80>
    ServerName my.example.com
    ServerAlias *.my.example.com
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/index_new.php
    RewriteRule ^(.*)$ /index_new.php [R=301,L]
</VirtualHost>

Notes:

  1. the site's .htaccess file is empty
  2. the "index_new.php" page is static with no PHP header("location:") internal redirects, etc.
  3. I have restarted apache
H. Ferrence
  • 7,906
  • 31
  • 98
  • 161
  • The rule looks good, you might omit the RewriteCond and shorten it to `RewriteRule !^/index_new.php$ /index_new.php [R,L]`. Never test with [R=301](http://stackoverflow.com/questions/9153262/tips-for-debugging-htaccess-rewrite-rules/9204355#9204355) – Olaf Dietsche Jun 07 '16 at 15:19
  • Thanks @OlafDietsche. But your answer still results in `my.example.com redirected you too many times.` in my Chrome browser (not that the browser has anything to do with it -- merely showing you the format of the resulting message). – H. Ferrence Jun 07 '16 at 15:28
  • I just tried this locally and both, your version and mine, work properly. Maybe there's something else. – Olaf Dietsche Jun 07 '16 at 16:01
  • Ok thanks @OlafDietsche. I'll have a look around. – H. Ferrence Jun 07 '16 at 17:17
  • I think the problem was that there is a css stylesheet ``'d in the `` container. Once I `RewriteCond`'d it I was good to go. – H. Ferrence Jun 07 '16 at 20:32

1 Answers1

0

A Negative lookahead should do, and can easily be done with a single line with RedirectMatch instead of using mod_rewrite:

RedirectMatch permanent ^/(?!index_new.php).* /index_new.php

Clear your browsers cache and/or try with curl -I http://my.example.com/wahtever to make sure it is working

For sanity specify documentroot in the virtualhost and a directory entry to allow access (Require all granted / Allow from all depending on the version) and since you are not using .htaccess (btw congratulations! you are one of the few doing things right from the start), set the directory of the DocumentRoot with "AllowOverride none" just to be safe.

Daniel Ferradal
  • 2,727
  • 1
  • 13
  • 19