简体   繁体   English

.htaccess:301 重定向在重写的根路径中的 .htaccess 中不起作用

[英].htaccess: 301 redirect not working in .htaccess in rewritten root path

I have a domain called example.com which points to /public_html/ on my server.我有一个名为example.com的域,它指向我服务器上的/public_html/

In /public_html/.htaccess I have the following code to change the root path for my domain example.com to /public_html/example_path/ :/public_html/.htaccess我有以下代码将我的域example.com的根路径更改为/public_html/example_path/

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule !example_path/ /example_path%{REQUEST_URI} [L]

This is working very well.这工作得很好。

Now, I want to redirect example.com/test.png to example.com/images/test.png .现在,我想将example.com/test.png重定向到example.com/images/test.png To do this, I'm using the following code in /public_html/example_path/.htaccess :为此,我在/public_html/example_path/.htaccess使用以下代码:

RewriteEngine On
Redirect 301 /test.png /images/test.png

Unfortunately, this isn't working, there's no redirection.不幸的是,这不起作用,没有重定向。 The redirect is only working if I put the code in /public_html/.htaccess but not in /public_html/example_path/.htaccess .只有当我将代码放入/public_html/.htaccess而不是/public_html/example_path/.htaccess ,重定向才有效。

Why that?为什么?

Inside your /public_html/.htaccess have redirect rule before example_path rule:在你的/public_html/.htaccessexample_path规则之前有重定向规则:

RewriteEngine On

RewriteRule ^test\.png$ /images/test.png [L,NC,R=301]

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule !example_path/ /example_path%{REQUEST_URI} [L,NC]

Note that you cannot have image redirection rule inside /example_path/.htaccess because your image URL doesn't have /example_path/ .请注意,您不能在/example_path/.htaccess设置图像重定向规则,因为您的图像 URL 没有/example_path/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM