Mod_Rewrite提供500服务器错误.htaccess

时间:2012-08-12 22:36:02

标签: php apache mod-rewrite

我不知道为什么,但是当我在.htaccess文件中使用跟随RewriteRule时,我收到内部服务器错误500.我启用了mod_rewrite我一直在寻找解决方案。

# Enable Rewriting  
RewriteEngine on  
# Rewrite user URLs 
RewriteRule ^(.*)/?$ users\/$1\/index.php

3 个答案:

答案 0 :(得分:1)

很可能500错误是由于重定向循环造成的。

您将 ANY 请求重写为users/$1/index.php,包括该URL本身,以便您最终获得重写循环。

尝试将重写规则更改为:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/users
RewriteRule ^(.*)/?$ users/$1/index.php

通过检查URI不以/users开头,您可以避免重写循环。

答案 1 :(得分:0)

Apache的错误日志(通常名为errors.log)可能提供有关其无效的原因的更多信息。很可能正则表达式由于某种原因是无效的,我将自己测试它然后编辑这篇文章。

答案 2 :(得分:0)

你有一个内部重写循环。重写引擎获取URI并将其放入引擎并获取生成的URI。如果它们是相同的,则重写引擎停止,否则,它将获取它返回的并通过引擎将其放回。这意味着您的/users/something/index.php再次与^(.*)/?$匹配并变为:/users/users/something/index.php/index.php,然后/users/users/users/something/index.php/index.phpusers/something/index.php/index.php等等。

因此,您需要添加RewriteCond以防止URI在以users/开头时重新被重写:

# Enable Rewriting  
RewriteEngine on  
# Don't rewrite if URI starts with "users"
RewriteCond %{REQUEST_URI} !users/
# Rewrite user URLs 
RewriteRule ^(.*)/?$ users\/$1\/index.php