开发者

.Htaccess rules to redirect respective HTTP links to HTTP and HTTPS to HTTPS?

开发者 https://www.devze.com 2023-04-11 15:14 出处:网络
First, here\'s the .htaccess rule I currently use: Options +FollowSymLinks RewriteEngine on RewriteRule (.*) http://example.com/$1 [R=301,L]

First, here's the .htaccess rule I currently use:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://example.com/$1 [R=301,L]

This is great, and redirects every link of my old domain to the very respective link in new domain. That is, http://olddomain.com/1.html is redirected to http://example.com/1.html and so forth.

But it doesn't redirect https://olddomain.com/1.html to https://example.com/1.html

And just so you know I tried, below are the rules I also happened to test. Unfo开发者_高级运维rtunately they're creating some kind of loop, and the redirection doesn't work.

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) example.com/$1 [R=301,L]

and

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} =on
RewriteRule (.*) example.com/$1 [R=301,L]

So, can someone give me the rules redirect http pages to http and https pages to https? Thanks.


#if https on
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
#else
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]


Your HTTPS rewritecond is incorrect. Cond is a regex, not an quality test. It should be

RewriteCond %{HTTPS} !^on

The %{HTTPS} var will only ever contain on or off. Never =on, so the match fails and triggers the redirect, even if https really is on.


update

For an unconditional HTTP->HTTPS redirect you'd need to redirect to an https URL. Your version just detects if HTTPS is *OFF, then redirects to the same url, causing a loop. What you need is:

RewriteCond %{HTTPS} !^on
RewriteRule (.*) https://example.com/$1 [R=301,L]
0

精彩评论

暂无评论...
验证码 换一张
取 消