简体   繁体   English

.htaccess RewriteRule更改目录以查询字符串500内部服务器错误

[英].htaccess RewriteRule Change Directory To Query String 500 Internal Server Error

I have the following code in my .htaccess file, which is mean to convert a directory to a query string parameter: 我的.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]

So, http://example.com/g/SOMETEXT should get rewritten as http://example.com/g/index.php?u=SOMETEXT 因此, http://example.com/g/SOMETEXT应该重写为http://example.com/g/index.php?u=SOMETEXT

As it stands, it works fine. 就目前而言,它工作正常。

However, if I uncomment out the commented line, I get a 500 Internal Server Error. 但是,如果我取消注释行,则将收到500 Internal Server Error。

That line is meant to rewrite one specific URL, http://example.com/g/XXXXXXXXXX-WINEGIFTS1 , as http://example.com/g/index.php?u=XXXXXXXXXX-WINEGIFTS 该行旨在将一个特定的URL http://example.com/g/XXXXXXXXXX-WINEGIFTS1重写为http://example.com/g/index.php?u=XXXXXXXXXX-WINEGIFTS

The weird thing is that I have the exact same code running on a different domain, on a different server, and it works fine. 奇怪的是,我在不同的域,不同的服务器上运行了完全相同的代码,并且工作正常。

Any thoughts on what's causing this error? 对导致此错误的原因有何想法?

Thanks! 谢谢!

The problem is that if you uncomment the first rule then the second rule becomes unconditional and it matches /g/index.php twice and rewrites to inself causing an endless rewrite loop . 问题是,如果您取消注释第一条规则,那么第二条规则将成为无条件的,并且它两次匹配/g/index.php并重写为自身,从而导致无休止的重写循环。

You can use the following 您可以使用以下内容

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM