【问题标题】:Mod_rewrite working on localhost, but not on GoDaddy shared hostingMod_rewrite 在本地主机上工作,但不在 GoDaddy 共享主机上
【发布时间】:2019-04-01 11:18:07
【问题描述】:

基本上,我正在尝试清理我的 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]

我在这里做错了什么?

【问题讨论】:

  • 我应该提到所有其他语句都在实时服务器上运行(404 页面、删除 index.ext 和训练斜线)。我只面临 URL 重写的问题。
  • 尝试禁用MultiViews 选项。
  • 感谢您的回复。我在 .htaccess 文件的顶部添加了Options -MultiViews,但这并没有解决问题。

标签: apache .htaccess mod-rewrite


【解决方案1】:

.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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 2018-10-27
    • 2011-07-25
    • 2020-11-03
    相关资源
    最近更新 更多