[英]Why is my .htaccess rewrite rule redirecting to the wrong place
这是我的 .htaccess 文件:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
RewriteRule ^(.*)$ https://www.myglobalsite.com/$1 [R,L]
## DIRECTORY
Options +FollowSymLinks
Options All -Indexes
DirectoryIndex index.php
ErrorDocument 404 https://www.myglobalsite.com/
redirectMatch 301 ^/products$ https://www.myglobalsite.com/en/product/drinking-cups
redirectMatch 301 ^/products/vending.php$ https://www.myglobalsite.com/en/product/drinking-cups
redirectMatch 301 ^/products/drinking.php$ https://www.myglobalsite.com/en/product/drinking-cups
问题是当它重定向时,它重定向如下:
来自: https://www.abc-something-old.com/products/vending.php
至: https://www.myglobalsite.com/products/vending.php
应该重定向到: https://www.myglobalsite.com/en/product/drinking-cups
我的重写规则有问题吗?
配置文件中的指令(“.htaccess”文件是“分布式配置文件”)从上到下处理。
第一个重定向规则将所有请求重定向到新域上的相同资源路径。 这意味着您进一步向下的RedirectMatch
指令永远不会发挥作用。
您必须更改指令的顺序,以便专门化、更精确的规则 go 到顶部以首先应用。 也不要忘记正确输入指令的名称,所以RedirectMatch
而不是redirectMatch
。 更通用的规则,因此通常将所有规则 go 放到底部,如果之前没有应用专门的规则,则作为最后一个选项应用。
## DIRECTORY
Options +FollowSymLinks
Options All -Indexes
DirectoryIndex index.php
ErrorDocument 404 https://www.myglobalsite.com/
RedirectMatch 301 ^/products$ https://www.myglobalsite.com/en/product/drinking-cups
RedirectMatch 301 ^/products/vending.php$ https://www.myglobalsite.com/en/product/drinking-cups
RedirectMatch 301 ^/products/drinking.php$ https://www.myglobalsite.com/en/product/drinking-cups
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
RewriteRule ^ https://www.myglobalsite.com%{REQUEST_URI} [R,L]
到目前为止,您混合使用了别名模块和重写模块的指令。 你可以统一这一点,尽管这两种方法都有效:
## DIRECTORY
Options +FollowSymLinks
Options All -Indexes
DirectoryIndex index.php
ErrorDocument 404 https://www.myglobalsite.com/
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
RewriteRule ^/?products/?$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
RewriteRule ^/?products/vending\.php$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
RewriteRule ^/?products/drinking\.php$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?abc-something-old\.com [NC]
RewriteRule ^ https://www.myglobalsite.com%{REQUEST_URI} [R,L]
顺便说一句:如果两个域都由同一个 http 服务器提供服务,并且评估同一个配置文件以处理请求,则只需要RewriteCond
。 否则,如果这些是单独的服务器或带有DOCUMENT_ROOT
文件夹的单独虚拟主机(我强烈推荐),您可以简化它:
## DIRECTORY
Options +FollowSymLinks
Options All -Indexes
DirectoryIndex index.php
ErrorDocument 404 https://www.myglobalsite.com/
RewriteEngine On
RewriteRule ^/?products/?$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
RewriteRule ^/?products/vending\.php$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
RewriteRule ^/?products/drinking\.php$ https://www.myglobalsite.com/en/product/drinking-cups [R,L]
RewriteRule ^ https://www.myglobalsite.com%{REQUEST_URI} [R,L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.