【问题标题】:Redirect all pages to the home except two pages将除两个页面外的所有页面重定向到主页
【发布时间】:2016-10-17 16:33:28
【问题描述】:

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

例如:

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-scripts。

所有可用的 URL 都指向 /(index.php 文件)

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

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

    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]
    

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

    【讨论】:

    • 这个行得通,但是需要把mysite.loc/send显示index.php的内容
    • 对不起,mysite.loc/1 或 mysite.loc/2 显示 index.php
    • 三个地址 1) mysite.loс 2) mysite.loс/1 3) mysite.loс/2 必须显示一个和来自文件index.php的内容。
    • 重定向到 /。因为这个 URL 不是 / 或 /1 或 /2。
    • 工作!非常感谢你今天帮助我。想了几天这个问题。
    【解决方案2】:

    这样的事情应该可以工作:

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

    【讨论】:

    • 服务器将请求转发到这个地址,这样它就永远不会结束。我有同样的错误:)
    • 很可能需要 RewriteCond% {ENV: REDIRECT_STATUS} ^$。但只要我不明白如何应用它。
    猜你喜欢
    • 2016-11-30
    • 1970-01-01
    • 2016-11-08
    • 2020-03-11
    • 1970-01-01
    • 2014-07-22
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多