簡體   English   中英

.htaccess 重定向 301:多個斜杠問題

[英].htaccess redirect 301: issue with multiple slashes

我必須在我的網站上進行一些重定向。 去年我從 WP 遷移到 Jekyll,所以目錄發生了變化,特別是關於圖像位置和類別。

首先,我從 http 重定向到 https。 然后,從 www 到非 www。 然后,我刪除了index.html 然后,我刪除多個斜杠:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*)
RewriteRule ^ %1/%2 [R=301,L]
</ifModule>

最后,我做了一些特定的 301 重定向。 例如,這個:

Redirect 301 /wp-content/uploads/2012 /

結果是domain/2012而不是domain ...

如果我嘗試做這樣的事情:

Redirect 301 /wp-content/uploads/2012/ /

結果是域//

我怎樣才能解決這個問題? 這個對嗎?

# Redirect HTTP to HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# Remove www subdomain
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>

# Remove index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
</ifModule>

# Remove multiple slashes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*)
RewriteRule ^ %1/%2 [R=301,L]
</ifModule>

您可以在您的站點根目錄 .htaccess 中嘗試這些規則:

RewriteEngine On

## add https, remove www and index.html in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{THE_REQUEST} /index\.html[?\s] [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*?)(?:index\.html)?$ http://%1/$1 [L,R=301,NE,NC]

# remove multiple slashes from URL
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule ^.*$ /$0 [R=301,L,NE]

# specific 301 redirects with optional trailing slash
RewriteRule ^wp-content/uploads/2012/?$ /? [L,R=301]

在本地 Apache 上測試此 .htaccess 之前,請確保完全清除瀏覽器緩存。

暫無
暫無

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

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