2017-03-04 33 views
1

我想匹配一個簡單的規則來重寫一個url,但它只是不匹配。我想 https://example.com/web/thanks/ 重定向到 https://example.com/thanks.phphtaccess - 簡單的重寫規則不觸發

這是我已經試過

RewriteEngine On 
RewriteBase/
RewriteRule ^thanks/$ https://example.com/thanks.php [R=302,L] 

RewriteEngine On 
RewriteRule ^thanks/$ https://example.com/thanks.php [R=302,L] 

RewriteEngine On 
RewriteBase/
RewriteRule ^/(.*)/thanks/$ https://example.com/thanks.php [R=302,L] 

RewriteEngine On 
RewriteBase/
RewriteRule ^web/thanks/$ https://example.com/thanks.php [R=302,L] 

和許多微小的變化,但他們沒有被觸發。我嘗試使用this online tool,它返回「此規則未得到滿足」。我究竟做錯了什麼?

+0

你真的想重定向而不是重寫嗎? 「不觸發」是什麼意思?你會得到什麼,404沒有找到,500服務器錯誤,或其他? –

+0

最後一個應該工作,順便說一句。至少應該將客戶端重定向到PHP文件。 –

+0

由於https://example.com/web/thanks/實際上並不存在,我得到了404。通過不觸發,我的意思是規則不匹配,我不會重定向。 – VeeK

回答

1

要重寫,只需用你的最後一條規則

RewriteRule ^web/thanks/?$ /thanks.php [L] 

有以下改動

  • 沒有RewriteBase,這只是一些相對URL

  • 可選尾隨斜線/?相關,如果你想同時使用/web/thanks/web/thanks/工作

  • 沒有域名,因爲這可能會引發重定向,而不是重寫,看到RewriteRule

    絕對URL
    如果指定一個絕對的URL,mod_rewrite的檢查,看是否主機名匹配當前主機。如果是這樣,則會刪除方案和主機名,並將生成的路徑視爲URL路徑。否則,將爲給定的URL執行外部重定向。要強制外部重定向回到當前主機,請參見下面的[R]標誌。

  • 沒有R|redirect標誌,因爲這會觸發一個重定向,而不是重寫


模式^.*thanks/$^(.*)thanks/$也可以,但是它在thanks/結束,像/hellothanks/任何URL匹配, /areyousurethanks//some/path/thanks/,...

+0

謝謝奧拉夫,我需要這個動作,所以我會保留'^(。*)謝謝/ $'。我刪除了'R'來刪除重定向 – VeeK