【问题标题】:.htaccess RewriteRule Change Directory To Query String 500 Internal Server Error.htaccess RewriteRule 更改目录以查询字符串 500 内部服务器错误
【发布时间】:2016-03-31 22:22:54
【问题描述】:

我的 .htaccess 文件中有以下代码,这意味着将目录转换为查询字符串参数:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^g/XXXXXXXXXX-WINEGIFTS1$ g/index.php?u=XXXXXXXXXX-WINEGIFTS [L,QSA]
RewriteRule ^g/(.*)$ g/index.php?u=$1 [L,QSA]

所以,http://example.com/g/SOMETEXT 应该被重写为 http://example.com/g/index.php?u=SOMETEXT

就目前而言,它工作正常。

但是,如果我取消注释掉注释行,我会收到 500 Internal Server Error。

该行旨在将一个特定的 URL http://example.com/g/XXXXXXXXXX-WINEGIFTS1 重写为 http://example.com/g/index.php?u=XXXXXXXXXX-WINEGIFTS

奇怪的是,我在不同的域、不同的服务器上运行完全相同的代码,并且运行良好。

对导致此错误的原因有什么想法吗?

谢谢!

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    问题是,如果您取消注释第一条规则,那么第二条规则将变为无条件,它匹配 /g/index.php 两次并重写为 inself 导致无限的重写循环。

    你可以使用下面的

    Options +FollowSymLinks
    RewriteEngine On
    ##--Skip the all rules bellow if an existent file/dir is requested--##
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    #########
     RewriteRule ^g/XXXXXXXXXX-WINEGIFTS1$ g/index.php?u=XXXXXXXXXX-WINEGIFTS [L,QSA]
    RewriteRule ^g/(.*)$ g/index.php?u=$1 [L,QSA]
    

    【讨论】:

    • 谢谢。我理解你所说的关于循环的内容,我认为(虽然我认为 L 属性会阻止这种情况),但我仍然收到上述代码的 500 错误。
    • @Mark L 标志不会停止规则处理,它只是告诉引擎退出当前的重写周期并转发第二阶段处理的请求
    猜你喜欢
    • 2014-01-01
    • 1970-01-01
    • 2015-09-30
    • 2013-06-11
    • 2016-09-22
    • 2017-04-05
    • 1970-01-01
    • 2013-02-06
    • 2015-06-28
    相关资源
    最近更新 更多