簡體   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