繁体   English   中英

将除两页外的所有页面重定向到主页

[英]Redirect all pages to the home except two pages

根目录是文件index.php和.htaccess配置文件。 如何使301重定向/(主页)中的所有URL,但使用字符串http://mysite.loc/1、http://mysite.loc/2和http://mysite.loc / ?

例如:

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 /)

问题在于无限重定向到主页。

注意:您必须仅在配置文件.htaccess中创建规则,不能使用PHP脚本。

所有可用的URL导致/(index.php文件)

这样的事情应该起作用:

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

您可以将此规则用作第一条规则:

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]

在测试此更改之前,请清除浏览器缓存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM