【问题标题】:using Mod Rewrite in .htaccess to get "prettify" urls working correctly在 .htaccess 中使用 Mod Rewrite 以使“美化”网址正常工作
【发布时间】:2017-12-05 10:12:10
【问题描述】:

我有一个网站,其中有几个页面是通过从数据库中提取数据生成的,查看这些页面的链接如下所示。

www.mywebsite.com/article.php?id=01&title=title

我正在尝试设置一个 mod 重写来让服务器显示相同的网页,但使用一个更好看的 url。

www.mywebsite.com/article/01/title

我在 .htaccess 文件中使用的代码行是

RewriteRule ^article/([0-9]+)/([A-Za-z0-9-\+]+)/?$ /article.php?id=$1&title=$2 [NC,L,QSA]

现在当我返回我的网站并输入以下网址时 www.mywebsite.com/article/01/title

它显示了正确的内容,但页面上根本没有附加样式?

我的完整.htaccess如下

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
# Needed before any rewriting
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(default|index)\.   (html|php|htm)\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*)(default|main|index)\.(html|php|htm)$ http://www.example.com/$1 [R=301]
RewriteRule ^article/([0-9]+)/([A-Za-z0-9-\+]+)/?$ /article.php?id=$1&title=$2 [NC,L,QSA]

有人可以告诉我我做错了什么吗? 非常感谢

【问题讨论】:

  • 你使用 CSS 文件的相对路径吗?
  • 在页面 HTML 的 &lt;head&gt; 部分下方添加:&lt;base href="/" /&gt;

标签: php .htaccess mod-rewrite url-rewriting


【解决方案1】:

这意味着你可能附加了一个不是以“/”开头的相对路径的 css 文件,请更改此设置

<link rel="stylesheet" type="text/css" href="style.css">
<!--- TO -->
<link rel="stylesheet" type="text/css" href="/style.css">
<!-- OR -->
<link rel="stylesheet" type="text/css" href="/path/to/css/style.css">

我认为您的重写规则没有问题,您现在必须管理您的 URL 中的虚拟目录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 2016-10-19
    相关资源
    最近更新 更多