簡體   English   中英

為什么我的 .htaccess 重寫規則重定向到錯誤的地方

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM