mod_rewrite规则500内部服务器错误

时间:2013-03-22 20:39:35

标签: php apache .htaccess mod-rewrite

我想从扩展程序网址重定向到无扩展程序网址。例如,/ contact应该读取/contact.php的内容。我使用以下规则来实现这一目标:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

有一个名为test.php的文件,它有一个名为“id”的变量。我想将读取请求从/ test / 1重定向到test.php?id = 1

我使用以下内容来实现此目的:

RewriteRule ^test/([0-9]+)/?$            /test?id=$1      

由于某种原因,调用test / 1文件会导致500内部服务器错误。如果我删除隐藏php扩展的规则,它会再次起作用

另外,如何强制对带扩展名的网址进行永久重定向?例如,如果有人联系到/contact.php应该重定向到/ contact

谢谢

2 个答案:

答案 0 :(得分:1)

您可以使用此代码:

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

# your existing rule (fixed)
RewriteRule ^test/([0-9]+)/?$ /test.php?id=$1 [L,NC,QSA]

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]

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

答案 1 :(得分:0)

这是在我的htaccess ...确保在Apache中启用mod_rewrite ...

RewriteEngine On
# Redirect .php requests to url without an extension
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
RewriteRule ^([^/.]+)$ $1.php [L]
# This will remove the trailing slash unless it's a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]