【问题标题】:Redirection loop URL rewriting重定向循环 URL 重写
【发布时间】:2015-11-03 17:21:50
【问题描述】:

我正在将所有 .php 扩展名重定向到 .html,但我遇到了循环问题。

例如来自:

-mywebsite.com/country/france-php/paris.php

-mywebsite.com/country/france-php/paris.html

这是我的 .htaccess:

RewriteEngine On
RewriteBase /    
RewriteRule (.+)\.php$ $1.html [R=301,L]
RewriteRule (.+)\.html$ $1.php [L] 
FallbackResource /index.php

当使用重定向侦探进行分析时,我得到了很多重定向到:

-mywebsite.com/country/france-php/paris.html

【问题讨论】:

    标签: mod-rewrite url-rewriting url-redirection friendly-url


    【解决方案1】:

    根据收到的原始请求进行第一次重定向:

    RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php
    RewriteRule ^ /%1.html [R=301,L]
    
    RewriteRule ^(.+)\.html$ /$1.php [L]
    

    要将index.php 也加载为index.html,您可以在顶部添加以下重写:

    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{REQUEST_URI} ^(.*)/$
    RewriteRule ^ %1/index.php [R=301,L]
    

    【讨论】:

    • 适用于:-mywebsite.com/country/france-php/paris.php 和 -mywebsite.com/country/france-fcret/ 但不适用于 -mywebsite.com/country/france- php/
    • @IssamHmz mywebsite.com/country/france-php/ 会发生什么?
    • 在 france-php 文件中有一个 index.php,因此它必须打开它,但是使用您的解决方案它不会打开它,只有打开 france-xrf france -fff 的文件
    【解决方案2】:

    你可以这样试试你的规则。

    RewriteEngine On
    RewriteBase /    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /(.+)\.php
    RewriteRule ^ %1.html? [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.+)\.html$ $1.php [L] 
    

    【讨论】:

    • 适用于:-mywebsite.com/country/france-php/paris.php 和 -mywebsite.com/country/france-fcret/ 但不适用于 -mywebsite.com/country/france- php/
    【解决方案3】:

    试试:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.php([^\?\ ]*)
    RewriteRule ^ %1.html%2 [L,R=301]
    
    RewriteCond %{DOUMENT_ROOT}/$1.php -f
    RewriteRUle ^(.+)\.html(?:(/.*)|)$ $1.php$2 [L]
    

    问题在于重写引擎循环并且您的两条规则不断相互覆盖。通过添加一些条件,您可以防止这种情况发生。

    【讨论】:

    • 适用于:-mywebsite.com/country/france-php/paris.php 和 -mywebsite.com/country/france-fcret/ 但不适用于 -mywebsite.com/country/france- php/
    猜你喜欢
    • 2015-09-08
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多