[英]IIS Url Rewite: Add Trailing Slash, Preserve Anchors and Query Strings
[英]Nginx rewrite: add trailing slash, preserve anchors and query strings
我需要確保給定站點上的所有永久鏈接都以尾部斜杠結尾。 也就是說,任何引用與實際單個文件不對應的位置的URL。 我還需要保留隨URL傳遞的任何查詢字符串和/或錨點。
假設我在以下位置有一個頁面:
example.com/about/
如果我收到以下請求,我希望它們重寫如下所示:
example.com/about
> example.com/about/
example.com/about?src=fb
> example.com/about/?src=fb
example.com/about#contact
> example.com/about/#contact
example.com/about#contact?src=fb
> example.com/about/#contact?src=fb
但是 ,我想確保我不會重寫任何實際的文件路徑 - 任何具有文件擴展名的文件。
這是我到目前為止提出的正則表達式,它只解決了排除真實文件路徑的問題,並在字符串末尾沒有一個時添加了一個尾部斜杠:
^([^\.]*$(?<!\/))
我還沒有弄清楚如何確定當存在錨點或查詢字符串時是否存在尾部斜杠,並且一旦確定如何分別捕獲應該在尾部斜杠之前和之后的部分以便組裝最后的重寫。
事實證明,我提出的正則表達式實際上解決了我所有的重寫需求。 這是我的Nginx server
配置的最終結果:
location / {
try_files $uri $uri/ @rewrites;
}
# Rewrite rules to sanitize and canonicalize URLs
location @rewrites {
rewrite ^([^\.]*$(?<!\/)) $1/ last;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.