fedor-sg
fedor-sg

Reputation: 229

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)

Upvotes: 2

Views: 240

Answers (2)

anubhava
anubhava

Reputation: 785856

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.

Upvotes: 0

clemep
clemep

Reputation: 124

Something like this should work:

RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteCond %{REQUEST_URI} !/(1|2) [NC]
RewriteRule ^(.*)$ / [L,R=301]

Upvotes: 0

Related Questions