.htaccess - localhost vs server

时间:2013-09-07 17:41:12

标签: regex apache .htaccess mod-rewrite

我有.htacces这样:

ErrorDocument 404 /error/404.php

Options +FollowSymlinks

RewriteEngine On

RewriteRule articles/(.*)-(.*) articles.php?$2=$1
RewriteRule download/(.*) download.php?q=$1

#add extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

现在,我的问题是:在localhost上工作正常,但在服务器上(启用mod_rewrite,检查),它会抛出500错误。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

像这样完成.htaccess:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^articles/([^-]+)-(.*)$ articles.php?$2=$1 [L,QSA,NC]
RewriteRule ^download/(.*)$ download.php?q=$1 [L,QSA,NC]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

答案 1 :(得分:0)

观看您的apache错误日志。您会注意到.php越来越多的重写。问题是,如果文件不存在,它将继续添加.php,直到它出现“内部重写太多”错误。您需要排除请求中已有.php扩展名的所有情况:

#add extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule (.*) $1.php [L]

要使您的上一条规则在其他规则上正常运行,您的其他规则也需要[L]标志。这将使apache通过变量中的正确信息再次通过.htaccess

RewriteRule articles/(.*)-(.*) articles.php?$2=$1 [L]
RewriteRule download/(.*) download.php?q=$1 [L]