简体   繁体   中英

.htaccess redirect 301: issue with multiple slashes

I have to make some redirects in my website. I migrated from WP to Jekyll last year, so directories changed, specially about images location and categories.

First, I redirect from http to https. Then, from www to non-www. Then, I remove the index.html . Then, I remove multiple slashes:

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

Finally, I do some specific 301 redirections. For example, this one:

Redirect 301 /wp-content/uploads/2012 /

The result is domain/2012 instead of domain ...

If I try to do something like this:

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

The result is domain//

How can I fix this? Is this correct?

# 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>

You may try these rules in your site root .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]

Make sure you completely clear your browser cache before testing this .htaccess on your local Apache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM