CodeIgniter 3 htaccess www to non-www, old domain to new domain, http to https and skip index.php all-in-one

Post Photo
Friday, 18th September 2020

It was not easy. Having an htaccess for CodeIgniter 3 to do all this all-in-once was really hard to find out. But, finally it works like a charm for me

This is what I came up with.

The following htaccess works in three steps.

In the first codeblock it translates www to non-www and at the same time it redirects non-SSL to SSL (http to https).

In the second step it makes a wildcard redirection from my old blog domain to my new blog domain (tomsnews.net => tomsnews.io). The complete path is being translated.

Last but not least it skips index.php because CodeIgniter renders everything over index.php.

I have tested it for a week so far and it works really well.

Have fun.

# BEGIN Rewrite
<IfModule mod_rewrite.c>
    RewriteEngine on
    
    # Http to Https and www to non-www
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule .* https://%1%{REQUEST_URI} [L,R=301]
    
    # tomsnews.net to tomsnews.io
    RewriteCond %{HTTP_HOST} ^tomsnews\.net$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.tomsnews\.net$
    RewriteRule ^(.*)$ "https\:\/\/tomsnews\.io\/$1" [R=301,L]

    # remove index.php
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
# END Rewrite

 

Share Post

If you like this post please share with your friends