【问题标题】:mod_rewrite rule keeps in loopmod_rewrite 规则保持循环
【发布时间】:2013-11-25 11:30:02
【问题描述】:

我正在为 mod_rewrite 做一个非常简单的规则,但我得到了一个循环 以下是规则:

首先,如果我有类似的要求

index.php/SOMETHING.(txt,jpg,php)

我需要先检查/SOMETHING.(txt,jpg,png)是否存在并显示

如果文件不是index.php/SOMETING 并且是存在的真实路径,则显示它

如果不是...将其传递给index.php/SOMETHING.(txt,jpg,php) 并显示 index.php

如果我有一个不存在的 txt、jpg、php,这一切都有效,但最后一条规则

示例:http://domain.com/index.php/robots1.txt

文件不存在:/Applications/XAMPP/xamppfiles/htdocs/testredir/robots1.txt

适用于任何其他扩展...

RewriteEngine On
RewriteBase   /


RewriteCond %{REQUEST_URI}  ^index.php/.+$
RewriteRule ^index.php/(.+)\.jpg$ $1.jpg [NC,L]
RewriteRule ^index.php/(.+)\.txt$ $1.txt [NC,L]
RewriteRule ^index.php/(.+)\.php$ $1.php [NC,L]


#here I am missing a rule but if i put the following it will loop

#RewriteCond %{REQUEST_URI} -f
#RewriteRule .*  index.php/$0 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  ^index.php/.+$
RewriteRule .* index.php/$0 [PT]

【问题讨论】:

    标签: apache mod-rewrite url-rewriting apache2


    【解决方案1】:

    %{REQUEST_URI} 变量始终以 / 开头,并且您使用 ^index.php/.+$ 对其进行匹配,因此条件将始终为 false

    看起来你想要这样的东西:

    RewriteEngine On
    RewriteBase   /
    
    RewriteCond %{THE_REQUEST} \ /index\.php/(.+)\.(jpg|txt|php)
    RewriteRule ^ /%1.%2 [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ index.php/$1 [L,PT]
    

    【讨论】:

    • 哇,是的,它有效:) 如果文件不存在,我该怎么做才能真正重定向到 index.php/$1,如 domain.com/index.php/robots1.txt 转到 domain.com/robots1.txt 并且它应该保留它是这样的..
    • @PartySoft 这就是它应该做的,第一条规则将它重定向到/robots.txt,如果存在,第二条规则永远不会被应用。
    • 它确实会显示 robots.txt(如果存在),但如果 robots1.txt 不存在,它会在后台进入 index.php,但 url 将是 domain.com/robots1.txt,它不会保留为 index.php/robots1.txt
    • @PartySoft URL 将保持在/robots1.txt,但请求会以静默方式路由到index.php
    猜你喜欢
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    相关资源
    最近更新 更多