在.htaccess文件上使用RewriteRule时,我在网站上显示图像时遇到问题。
简而言之,我有很多页面包含我要转换的 GET 变量。例如:
http://example.com/home.php?page=categorie => http://example.com/categorie
http://example.com/home.php?page=categorie&act=view&lettera=A => http://example.com/categorie/A
我可以做到这一点,使用这个.htaccess。
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^categorie$ /home.php?page=categorie [L]
RewriteRule ^categorie/([^/]*)$ /home.php?page=categorie&act=view&lettera=$1 [L]
当我继续 http://example.com/categorie 没有任何问题。
当我继续 http://example.com/categorie/A 看不到任何样式,js,图像。
因此,我添加此行:
RewriteRule ^categorie/(css|img|jpeg|jpg|gif|png|js|svg)/(.*)?$ /$1/$2 [L,QSA,R=301]
当我继续时 http://example.com/categorie/A 只能看到css样式。知道吗?
页面HTML的<head>
部分添加以下内容:
<base href="/" />
以便从该 URL 而不是当前页面的 URL 解析每个相对 URL。
当你直接进入页面/home.php?page=categorie&act=view&lettera={val}
你能看到图像吗? 除非图像路径被引用为categorie/A
否则它应该保持不变。 您不需要定义文件/图像扩展名的最后一个规则。
转到包含问题categorie/A
的页面,并在 Firefox 中启动firebug
。检查缺少的图像并执行检查元素以调试图像未显示的原因,也许 url 以某种方式被重写了。
重写到不同的深度,相对 URL 也需要更改。因此,例如,如果<img src="../images/myimage.jpg">
在/home.php?...
上工作,则src需要更改为<img src="../../images/myimage.jpg">
以进行/category/A
。
最简单的选择是确保您的图像URL使用相对于根而不是文档的路径,即 <img src="/images/myimage.jpg">
.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z]+)$ user.php?username=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^country/([0-9a-zA-Z]+)$ country.php?countryname=$1 [L]
url1: http://domain.com/Rocky
url2: http://domain.com/country/India
Add <base href="../"> in country.php<head>
<head>
<base href="../">
</head>