在.htaccess中使用Mod Rewrite来获得“美化”网址正常工作(using Mod Rewrite in .htaccess to get “prettify” urls working correctly)

我有一个网站,其中有几个网页是通过从数据库中提取数据生成的,查看这些网页的链接看起来像这样。

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]

现在当我回到我的网站并输入以下URL 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]

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

I have a website which has several pages which are generated by pulling data from a database, the links to view those pages look like this.

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

and i am trying to set up a mod rewrite to get the server to display the the same webpages but using a much nicer looking url.

www.mywebsite.com/article/01/title

the line of code i have used in my .htaccess file is

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

now when i go back to my website and type in the following url www.mywebsite.com/article/01/title

it displays the correct content but there is no styling attached to the page at all?

my full .htaccess is as follows

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

can someone please tell me what i am doing wrong? many thanks

最满意答案

这意味着你可能已经将你的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中的虚拟目录。

That means you have probably attached your css file with a relative path not starting by a "/", change this

<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">

I don't think there is a problem with your rewrite rules, you just have virtual directories in your URL now you have to manage.

更多推荐