我在example.com上有一个非常简单的html页面。它仅由以下index.html
组成
<html>
<body style="background-color:black;">
<img src="images/picture1.jpg" alt="example" style="width:40%;">
</html>
当我转到example.com时,页面完全按照预期加载,但我想这样做,这样对example.com的任何请求都会被重定向到这个首页(因此example.com/catfood会将用户带到example.com)。为此,我在.htaccess
中添加了以下内容:
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/
RewriteRule ^ / [R=301]
它正确地重定向,但它破坏了图像。我相信这是因为对example.com/images/picture1.jpg的请求现在被重定向到了索引页面,但我不确定我需要在.htaccess中添加什么才能允许全面重定向发生,同时仍然在首页加载此图像。
发布于 2018-07-26 06:12:52
添加另一个RewriteCond,说明该规则仅在请求不指向现有文件时才适用。
RewriteCond %{REQUEST_FILENAME} !-f
所以:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/
RewriteRule ^ / [R=301]
请参阅https://httpd.apache.org/docs/current/mod/mod_rewrite.html
https://stackoverflow.com/questions/51528348
复制相似问题