简体   繁体   中英

Mod_rewrite http->https redirect EXCEPT one URL

OK, so I'm trying to do this:

Summary: all URLs should be https://www.domain.com/ (.*) except /[a-f0-9]{11}/, which should be forced to http only.

I have an old set of rules (see below) which don't look very clean. I've tried adding to them to account for /[a-f0-9]{11} and it ends up redirecting instead of remapping, so I end up with www.domain.com/index.php/thepattern0

How do I clean these rules up and make this work?

<IfModule mod_rewrite.c>
    Options +FollowSymLinks -MultiViews -Indexes
    RewriteEngine On
    RewriteBase /
    #redirect to www
    RewriteCond %{HTTP_HOST} ^domain.com [NC]
    RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

    #redirect to https
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    #hide index.php
    RedirectMatch 404 .*php\.ini
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    #RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

You can use:

Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /

#redirect to www
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#redirect to https
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+[a-f0-9]{11}[/?\s] [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]

#redirect to http
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+[a-f0-9]{11}[/?\s] [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]

#hide index.php
RewriteRule \.(?:php|ini)$ [L,R=404,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

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