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

我只是一个虾纸丫 提交于 2019-12-11 20:06:10

问题


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. Unfortunately 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.


回答1:


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



回答2:


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]


来源:https://stackoverflow.com/questions/7705153/htaccess-rules-to-redirect-respective-http-links-to-http-and-https-to-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!