dongshi1215 2017-12-05 10:12
浏览 38
已采纳

在.htaccess中使用Mod Rewrite来获得“美化”网址正常工作

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

  • 写回答

1条回答 默认 最新

  • doubu4826 2017-12-05 10:27
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办