php - 为什么我的 .htaccess 文件在 cPanel 中无法工作?

标签 php apache .htaccess mod-rewrite cpanel

我的 htaccess 是

Options -Multiviews

RewriteEngine On
RewriteBase /

# Force search engines to use www.mommy-info.com
RewriteCond %{HTTP_HOST} !^www\.mommy-info\.com$
RewriteRule ^(.*) http://www.mommy-info.com/$1 [R=301,L]

# Specify search friendly URLs
RewriteRule ^about/$ /about.php [L]

我已经保存了它,但它没有在我的网站上重写它。它仍然显示“丑陋”的网址。我不知道该怎么做,因为我对 htaccess 文件还很陌生,并且不确定为什么它不起作用。

如果我输入“漂亮”网址 (www.mommy-info.com/about/),它没有 CSS,但它是漂亮网址。如果我浏览网站上的菜单,它只会显示丑陋的网址。

最佳答案

当我访问您的网站时,似乎mod_rewrite正在工作,事情有点不对劲。

是否打中
http://www.mommy-info.com/about.php

http://www.mommy-info.com/about/
我看到了相同的内容,

这可以通过

看到
$ curl http://www.mommy-info.com/about/ | md5
1105b8f6dbf1a2c52bddd8e5f9046653

$ curl http://www.mommy-info.com/about.php | md5
1105b8f6dbf1a2c52bddd8e5f9046653

这里的问题实际上似乎与您的 main.css 资源有关,该资源被引用为:

<link href="main.css" type="text/css" rel="stylesheet"/>

问题是我请求/about/时,相对引用了css文件,失败;您可以通过在浏览器中检查并刷新 /about/ 页面来看到这一点,样式表将抛出 404。

$ curl --head http://www.mommy-info.com/about/main.css
HTTP/1.1 404 Not Found

要解决样式表问题,请更改代码行以从文档根目录访问它,方法是在路径前面添加 / ,这将访问它 statically vs relatively .

<link href="/main.css" type="text/css" rel="stylesheet"/>
           /\ access statically from root

除了样式表之外,您可能希望从 RewriteRules 中删除所有尾部斜杠,因为它会强制用户附加 / 或 404将会看到:

$ curl --head http://www.mommy-info.com/about
HTTP/1.1 404 Not Found

$ curl --head http://www.mommy-info.com/about/
HTTP/1.1 200 OK

关于php - 为什么我的 .htaccess 文件在 cPanel 中无法工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43418786/

相关文章:

php发送带附件的邮件

php - 是否有可能像使用tcpdump一样捕获php中的flv链接?

html - 如何从子目录中包含/链接 css(和 html)?

linux - Amazon Linux AMI Apache 用户和权限 Web 目录

tomcat 的 apache 代理保持活跃困惑

apache - 如何显示单个页面的 503 错误

php - 想要使用 composer 添加新插件而不是更新所有插件

php - Yii 主题不工作

php - .htaccess 重定向但排除某些文件夹

html - wordpress重定向到主页和seo