且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HTTP到HTTPS htaccess的重定向循环错误

更新时间:2022-10-29 12:17:45

您可以在您的根目录的.htaccess使用code:

  RewriteEngine叙述上
的RewriteBase /

#强制HTTPS和www
的RewriteCond%{HTTP:X-转发,SSL}上!
重写规则^ HTTPS://www.example.com% {REQUEST_URI} [L,NE,R = 302]

的RewriteCond%{HTTP_HOST}!^ WWW \。 [NC]
重写规则^ HTTPS://www.% {HTTP_HOST}%{REQUEST_URI} [L,NE,R = 302]

#跳过进一步的规则文件和目录
的RewriteCond%{} REQUEST_FILENAME -D [OR]
的RewriteCond%{} REQUEST_FILENAME -f
重写规则^  -  [L]

#内部重写规则
重写规则^([\ w  - ] +)/([\ w  - ] +)1 / $ page.php场地= $&放大器;?艺术家= $ 2 [L,QSA]
重写规则^([\ w  - ] +)?/ $ page.php场地= $ 1 [L,QSA]
 

I have a site which I am trying to completely redirect from http to https.

I need to redirect any user that arrives at the site via http to the same page on https.

The site is hosted on a provider's shared hosting. Since the domain's SSL certificate was installed, a new IP address has been assigned to the domain.

I have been advised that the following code should be added to my .htaccess file. There are also some rules which handle SEO friendly URLS.

RewriteEngine On
RewriteBase /

# enforce https and www
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,NE,R=302]

# skip further rules for files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# internal rewrite rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ page.php?venue=$1&artist=$2 [L,QSA]
RewriteRule ^([\w-]+)/?$ page.php?venue=$1 [L,QSA]

For some reason, the addition of this code to the .htaccess file, results in a redirection loop error. I have tried variants of the RewriteCond %{SERVER_PORT} !=443 line as advised on other Stack Overflow posts, but nothing seems to work.

Does anyone have a solution?

You can use this code in your root .htaccess:

RewriteEngine On
RewriteBase /

# enforce https and www
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,NE,R=302]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]

# skip further rules for files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# internal rewrite rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ page.php?venue=$1&artist=$2 [L,QSA]
RewriteRule ^([\w-]+)/?$ page.php?venue=$1 [L,QSA]