簡體   English   中英

mod_rewrite用於目錄的結尾斜杠,用於文件url的刪除

[英]mod_rewrite trailing slash for directory and remove for file url

好。 我遇到了這個問題,試圖刪除文件URL中的最后一個斜杠,例如http://domain.com/styles/styles.css/ 我得到了在末尾添加斜杠的代碼,但無法弄清楚如何做條件代碼。

如果網址有擴展名,則刪除斜杠,否則添加斜杠。

在這里,我現在得到的一些博客說了它的解決方案,但仍無法滿足我的期望。

RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

還有一個問題,當我輸入http://domain.com/index它會轉到http://domain.com/inde/ 需要您的幫助。。在此先多謝。

在您的htaccess中添加以下代碼,以更好地理解。

RewriteCond %{HTTP_HOST} !^\.yourdomain\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

I have a link : domain.com/folder/
it will change to : domain.com//folder

您還可以通過添加DirectorySlash Off來關閉mod_dir的重定向。

為什么要對此類“家具”文件進行外部重定向? 您肯定要在這里進行內部重定向嗎?

Options     -MultiViews

RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.+)/$        $1         [L]

我建議您如果不使用mutliviews,則應將其關閉,因為這會生成使事情變得混亂的子請求。

您的RewriteCond條件在邏輯上是相反的,因為您有! 操作員在那里。 因此,重寫僅適用於不具有擴展名且不帶斜杠的輸入!

您可以使用一條沒有條件的規則來做到這一點:

# Match any sequence of characters, ending in a dot followed
# by one or more characters that don't contain dots or slashes,
# followed by a final trailing slash.
#
# Rewrite this absolutely and treat as redirect.

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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