【问题标题】:Why is my .htaccess file not working in cPanel?为什么我的 .htaccess 文件在 cPanel 中不起作用?
【发布时间】:2017-09-11 03:23:36
【问题描述】:

我的 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 文件还很陌生,不知道为什么它不起作用。

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

【问题讨论】:

  • 确保在apache中启用了模块mod_rewrite

标签: php apache .htaccess mod-rewrite cpanel


【解决方案1】:

当我访问您的网站时,它确实看起来好像 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

【讨论】:

  • 谢谢!我已经修复了尾部斜杠,并且正在修复 css 相对访问。你知道为什么当我从菜单访问页面时它没有显示漂亮的网址吗?
  • 因为您的锚点设置为将用户重定向到 .php 文件。 &lt;a href="about.php"&gt;&lt;li&gt;About&lt;/li&gt;&lt;/a&gt; 改成&lt;a href="/about"&gt;&lt;li&gt;About&lt;/li&gt;&lt;/a&gt;
  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-24
  • 1970-01-01
  • 2012-02-17
相关资源
最近更新 更多