域重定向不是很完美

时间:2018-01-05 11:29:14

标签: .htaccess codeigniter url redirect url-redirection

我在codeigniter中设置了一个网站。在config.php中,baseurl是

https://example.com

在htaccess中,编写了以下代码:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://example.com/$1 [R,L]
php_value date.timezone Asia/Kolkata
<IfModule mod_rewrite.c>
 RewriteEngine on 
 RewriteCond $1 !^(index.php|resources|robots.txt) 
 RewriteCond %{REQUEST_FILENAME} !-f 
 RewriteCond %{REQUEST_FILENAME} !-d 
 RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 
</IfModule>
<IfModule mod_headers.c>
 SetEnvIf Origin "https://(www|sub1|sub2|sub3).example.com)$" ACAO=$0
 Header set Access-Control-Allow-Origin "%{ACAO}e" env=ACAO
 Header set Access-Control-Allow-Methods "GET"
</IfModule>

域名有四种变体:

  1. https://www.example.com

  2. http://www.example.com

  3. http://example.com

  4. https://example.com

  5. 我希望1,2和3重定向到4.对于htaccess代码,2和3重定向到4。

    但是,

    1未重定向到4

    请提出解决方案。

1 个答案:

答案 0 :(得分:2)

要删除www并强制执行https替换您的第一个重定向规则:

RewriteEngine On

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

RewriteCond $1 !^(index.php|resources|robots.txt) 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 

<IfModule mod_headers.c>
 SetEnvIf Origin "https://(www|sub1|sub2|sub3).example.com)$" ACAO=$0
 Header set Access-Control-Allow-Origin "%{ACAO}e" env=ACAO
 Header set Access-Control-Allow-Methods "GET"
</IfModule>