重定向太多。設置 htaccess 以重定向 (too many redirects. setting up htaccess to redirect)


問題描述

重定向太多。設置 htaccess 以重定向 (too many redirects. setting up htaccess to redirect)

我把一個舊網站移到了一個新的cms,有些內容的鏈接在新系統上找不到。

例如在舊系統中有一個鏈接叫做https://example.com/newsletter/1234/123 在新的我最後不需要這些 id . 因此,我想直接將用戶重定向到 https://example.com/newsletter/。 p>

我在我的 htaccess 文件中寫了以下內容:

RewriteRule  ^(.*newsletter)(.*) $1 [L,R=301]

不幸的是,這給了我一個“太男人的重定向”。錯誤。誰能指出我犯的錯誤?

我計劃使用正則表達式來捕獲包含名稱“newsletter”的所有鏈接。並刪除 URL 的其餘部分以重定向用戶。感謝任何幫助。


參考解法

方法 1:

Using (.*newsletter)(.*) causes the rewrite to match /newsletter alone because .* matches zero or more characters. This sends mod_rewrite into a loop.

Instead, you can use .+ which matches one or more characters, and to that I would prepend /? to optionally match a trailing slash. There is no need to capture it in () because you do not reuse it as $2.

RewriteRule ^(.*newsletter)/?.+ $1 [L,R=301]

Your example began with .*. If you actually need that, leave it in. But if you are really just trying to match /newsletter/123/1234 and not /someother/newsletter/123/1234, simplify it with:

RewriteRule ^newsletter/?.+ /newsletter [L,R=301]

When you test this, use a private/incognito browsing window or a fresh browser. Browsers aggressively cache 301 redirects and it can make it very hard to debug rewrite rules if you are fighting against the cache.

(by K NugalMichael Berkowski)

參考文件

  1. too many redirects. setting up htaccess to redirect (CC BY‑SA 2.5/3.0/4.0)

#.htaccess






相關問題

使用 htaccess 從基本 URL 中刪除變量 (Remove variable from base URL with htaccess)

多個相同的 url htaccess 重定向 (Multiple same url htaccess redirect)

使用 mod_rewrite 更改 URL 中的單詞的問題 (Issue changing a word in a URL using mod_rewrite)

無法在 XAMPP 中進行 301.htaccess 重定向 (Cannot 301.htaccess redirect in XAMPP)

Cách ẩn mọi thứ sau tên trang web của bạn bằng .htaccess (How to hide everything after your webpage name with .htaccess)

htaccess 顯示內部服務器錯誤 (htaccess show internal server error)

mod-rewrite 結合了兩種不同的重寫規則 (mod-rewrite combine two different rewrite rules)

安全的 PHP 文件上傳 (Secure PHP file uploading)

用 .htaccess 重寫基於日期的 URL:s - 解決方案? (Rewriting date-based URL:s with .htaccess - solution?)

htaccess 強制 https 和 www 到非 www,但子域僅 https (htaccess force https & www to non-www, but subdomain only https)

重定向太多。設置 htaccess 以重定向 (too many redirects. setting up htaccess to redirect)

.htaccess https、www 和子域靜默重寫 (.htaccess https, www and subdomain silent rewrite)







留言討論