RewriteRule htaccess 不显示图像
RewriteRule htaccess not show images
我在 .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/ [L,R=301]
RewriteRule ^categorie$ /home.php?page=categorie [L]
RewriteRule ^categorie/([^/]*)$ /home.php?page=categorie&act=view&lettera= [L]
当我继续http://example.com/categorie没有任何问题。
继续http://example.com/categorie/A看不到任何样式、js、图片。
所以,我添加了这一行:
RewriteRule ^categorie/(css|img|jpeg|jpg|gif|png|js|svg)/(.*)?$ // [L,QSA,R=301]
并且当我继续 http://example.com/categorie/A 时只能看到 css 样式。
有什么想法吗?
直接进入/home.php?page=categorie&act=view&lettera={val}
页面能看到图片吗?除非图像路径被引用为 categorie/A
,否则它应该保持不变。您不需要定义 file/image 扩展名的最后一条规则。
转到有问题的页面 categorie/A
并在 Firefox 中启动 firebug
。查看丢失的图像并执行检查元素以调试图像未显示的原因,也许 url 以某种方式被重写了。
您可以将其添加到您页面 HTML 的 <head>
部分:
<base href="/" />
这样每个亲戚 URL 都是从那个 URL 解析的,而不是当前页面的 URL。
通过将 URL 重写到不同的深度,相关的 URL 也需要更改。因此,例如,如果 <img src="../images/myimage.jpg">
在 /home.php?...
上工作,则 src 需要更改为 <img src="../../images/myimage.jpg">
以用于 /category/A
.
最简单的选择是确保您的图像 URLs 使用相对于根目录而非文档的路径,即 <img src="/images/myimage.jpg">
.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z]+)$ user.php?username= [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^country/([0-9a-zA-Z]+)$ country.php?countryname= [L]
url1: http://domain.com/Rocky
url2: http://domain.com/country/India
Add <base href="../"> in country.php<head>
<head>
<base href="../">
</head>
我在 .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/ [L,R=301]
RewriteRule ^categorie$ /home.php?page=categorie [L]
RewriteRule ^categorie/([^/]*)$ /home.php?page=categorie&act=view&lettera= [L]
当我继续http://example.com/categorie没有任何问题。
继续http://example.com/categorie/A看不到任何样式、js、图片。
所以,我添加了这一行:
RewriteRule ^categorie/(css|img|jpeg|jpg|gif|png|js|svg)/(.*)?$ // [L,QSA,R=301]
并且当我继续 http://example.com/categorie/A 时只能看到 css 样式。 有什么想法吗?
直接进入/home.php?page=categorie&act=view&lettera={val}
页面能看到图片吗?除非图像路径被引用为 categorie/A
,否则它应该保持不变。您不需要定义 file/image 扩展名的最后一条规则。
转到有问题的页面 categorie/A
并在 Firefox 中启动 firebug
。查看丢失的图像并执行检查元素以调试图像未显示的原因,也许 url 以某种方式被重写了。
您可以将其添加到您页面 HTML 的 <head>
部分:
<base href="/" />
这样每个亲戚 URL 都是从那个 URL 解析的,而不是当前页面的 URL。
通过将 URL 重写到不同的深度,相关的 URL 也需要更改。因此,例如,如果 <img src="../images/myimage.jpg">
在 /home.php?...
上工作,则 src 需要更改为 <img src="../../images/myimage.jpg">
以用于 /category/A
.
最简单的选择是确保您的图像 URLs 使用相对于根目录而非文档的路径,即 <img src="/images/myimage.jpg">
.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z]+)$ user.php?username= [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^country/([0-9a-zA-Z]+)$ country.php?countryname= [L]
url1: http://domain.com/Rocky
url2: http://domain.com/country/India
Add <base href="../"> in country.php<head>
<head>
<base href="../">
</head>