简体   繁体   中英

using htaccess to redirect http and https based on conditions

I want to redirect everything except an url to HTTPS and redirect the specific url to HTTP

I used the following apache config:

<IfModule mod_rewrite.c>
    RewriteCond %{SERVER_PORT} !^443
    RewriteCond %{REQUEST_URI} !^/url
    RewriteRule ^ https://mydomain.tld%{REQUEST_URI} [R=301,L]
</IfModule>

<IfModule mod_rewrite.c>
    RewriteCond %{SERVER_PORT} ^443
    RewriteCond %{REQUEST_URI} ^/url
    RewriteRule ^ http://mydomain.tld%{REQUEST_URI} [R=301,L]
</IfModule>

Unfotunately it just loop forever in the redirections and I can't figure out the correct rewritecond conditions to make it work properly

UPDATE :

I also have these rules beside that might influence something I guess... :

<IfModule mod_rewrite.c>
    Options -Multiviews +SymLinksIfOwnerMatch
    RewriteEngine On
    RewriteBase /
    DirectorySlash Off
</IfModule>

<IfModule mod_rewrite.c>
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>

<IfModule mod_rewrite.c>
    RewriteCond %{SCRIPT_FILENAME} -d [OR]
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule "(^|/)\." - [F]
</IfModule>

<IfModule mod_headers.c>
    Header set Strict-Transport-Security "max-age=16070400; includeSubDomains"
</IfModule>

<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

I think you need to rearrange your rules:

<IfModule mod_headers.c>
    Header set Strict-Transport-Security "max-age=16070400; includeSubDomains"
</IfModule>

DirectorySlash On
Options -Multiviews +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule !^url https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteRule ^url http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^ - [F]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,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