ITSagar
ITSagar

Reputation: 703

Domain redirection is not working perfect

I have a website setup in codeigniter. In config.php, baseurl is

https://example.com

In htaccess, following code has been written:

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>

There are four variations of domains:

  1. https://www.example.com

  2. http://www.example.com

  3. http://example.com

  4. https://example.com

I want 1, 2 and 3 redirect to 4. Witht he above htaccess code, 2 and 3 are redirecting to 4.

But,

1 is not redirecting to 4

please suggest a solution.

Upvotes: 1

Views: 31

Answers (1)

anubhava
anubhava

Reputation: 785186

To remove www and enforce https, replace your first redirect rule with this one:

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>

Upvotes: 2

Related Questions