http到https htaccess重定向循环错误

时间:2014-08-14 19:40:46

标签: .htaccess http redirect https

我有一个网站,我试图完全从http重定向到https

我需要将通过http到达该网站的所有用户重定向到https上的同一页面。

该网站托管在提供商的共享主机上。由于已安装域的SSL证书,因此已为域分配了新的IP地址。

我被告知应将以下代码添加到我的.htaccess文件中。还有一些处理SEO友好URL的规则。

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]

由于某种原因,将此代码添加到.htaccess文件会导致重定向循环错误。我已尝试过其他Stack Overflow帖子中建议的RewriteCond %{SERVER_PORT} !=443行的变体,但似乎没有任何效果。

有没有人有解决方案?

2 个答案:

答案 0 :(得分:0)

我从我的服务提供商处获得了这个代码,似乎使用php

<?php 
 if ($_SERVER['HTTP_X_FORWARDED_SSL'] == '1') {  } else {    $redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];    header("Location: $redirect"); } ?>

我可以为永久重定向添加以下内容

header("HTTP/1.1 301 Moved Permanently");

答案 1 :(得分:0)

您可以在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]
相关问题