我是.htaccess文件的新手。在我的网站上,我有一个页面供用户按类别搜索书籍并按一些参数排序。真实页面的URL是这样的。我已经使用了一些带有相对路径的css和js文件。例如:<link href="../css/mystyle.css" rel="stylesheet">
www.example.com/post/index.php?cat=science-fiction&sort=date
我已经使用.htaccess文件修改了该网址。以获得更有意义的网址体验。
#Turn Rewrite Engine On
RewriteEngine on
#Rewrite for ads.php
RewriteRule ^([0-9a-zA-Z_-]+) index.php?cat=$1 [NC,L]
我想要这样的网址。及其工作。
www.example.com/post/science-fiction
www.example.com/post/science-fiction?sort=date
但是我的问题是,当用户在网址末尾输入"/"
时。页面无法加载CSS或JS文件。我想重定向或忽略网址的"/"
端。
此网址存在问题
www.example.com/post/science-fiction /
www.example.com/post/science-fiction/?sort=date
如何解决此变化的.htaccess文件
答案 0 :(得分:3)
您可以在网站根.htaccess中使用以下规则:
RewriteEngine on
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ index.php?cat=$1 [QSA,L]
对于长期方法,请考虑在CSS中使用绝对路径,如下所示:
<link href="/css/mystyle.css" rel="stylesheet">