fedor-sg fedor-sg - 1 month ago 13
Apache Configuration Question

Redirect all pages to the home except two pages

The root directory is the file index.php and .htaccess configuration file. How to make a 301 redirect all URLs in the / (main page) but URL'a with the string http: //mysite.loc/1, http: //mysite.loc/2 and http: //mysite.loc /?

For example:

http: //mysite.loc/ (URL available, show index.php)
http: //mysite.loc/1 (URL available, show index.php)
http: //mysite.loc/2 (URL available, show index.php)
http: //mysite.loc/testing (301 redirect on /)
http: //mysite.loc/qwerty (301 redirect on /)


The problem lies in the infinite redirects to the main page.

Note: You must create a rule only in the configuration file .htaccess, PHP-scripts can not be used.

All available URL leads to / (index.php file)

Answer

You can use this rule as your very first rule:

RewriteEngine On

# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*/)?index\.php$ /$1 [L,R=301,NE]

RewriteRule !^(1|2|index\.php)?/?$ / [L,R=301]

RewriteRule ^(1|2)?/?$ index.php [L]

Clear browser cache before testing this change.

Comments