RewriteRule创建500内部服务器错误 [英] RewriteRule creating 500 Internal Server Error
问题描述
.htaccess文件中包含以下内容:
I have the following in my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^directory/(.*)$ directory/index.php?id=$1
我要实现的目标是:
访问URL www.mydomain.com/directory/10
时,浏览器上将显示页面www.mydomain.com/directory/?id=10
,而不会更改URL的外观.
When the URL www.mydomain.com/directory/10
is visited, the page www.mydomain.com/directory/?id=10
is displayed on the browser without altering the appearance of the URL.
上面的代码虽然会创建500个内部服务器错误.
The above code creates a 500 Internal server error though.
有人知道我要去哪里吗?
Does anyone know where I'm going wrong?
推荐答案
由于您的代码会导致无限循环,因此可以确保您的代码生成500个内部服务器错误.原因是您匹配的URI模式为:^directory/(.*)$
Your code is guaranteed to generate 500 internal server error because it is causing infinite looping. Reason is that your matching URI pattern is: ^directory/(.*)$
在重写前后匹配您的URL.并且一旦达到最大允许的内部重写限制,Apache就会抛出500个内部服务器错误并纾困.
Which matches your URLs before and after rewrites. And once it reaches max allowed internal rewrite limit Apache throws 500 internal server error and bails out.
将代码更改为此:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^directory/(.*)$ /directory/index.php?id=$1 [L,QSA,NC]
上面的代码还有一个额外的RewriteCond %{REQUEST_FILENAME} !-f
,这将确保在第一次之后不允许RewriteRule的后续执行,因为/directory/index.php
将是有效文件.
Above code has an extra RewriteCond %{REQUEST_FILENAME} !-f
that will make sure to disallow subsequent execution of RewriteRule after first time since /directory/index.php
will be a valid file.
这篇关于RewriteRule创建500内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!