Mod_rewrite可在localhost上运行,但不能在GoDaddy共享主机上运行

时间:2019-03-27 03:02:43

标签: apache .htaccess mod-rewrite

基本上,我正在尝试清理URL以删除文件扩展名。

例如,我有一个名为contact.php的文件。我想在浏览器中显示此页面,但我希望URL如下所示:

https://www.example.com/contact/

我也希望网站上的链接显示清理后的版本,因此在我的HTML中,我链接到此页面,链接为:

<a href="/contact/">Contact Us</a>

我在.htaccess中拥有的mod_rewrite规则在localhost上运行正常,但是当推送到GoDaddy共享托管服务器上的测试子域时,出现“重定向过多”错误。 .htaccess文件位于此子域的子文件夹中。

这是我的整个.htaccess文件:

RewriteEngine on

# custom 404 page
ErrorDocument 404 /404.php

# always remove index.ext
RewriteRule ^index\.(php|html)$ / [R=301,L]
RewriteRule ^(.*)/index\.(php|html)$ /$1/ [R=301,L]

# redirect somepage.ext to /somepage
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^/?(.*).(php|html)$ /$1/ [R=301,L]

# always make sure page has trailing slash
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]

我在这里做什么错了?

1 个答案:

答案 0 :(得分:0)

尝试以上.htaccess文件中的代码

RewriteEngine on

ErrorDocument 404 /404.php

RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]

RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]