Search code examples
.htaccessurl-rewritingno-www

www to non-www with SSL without index.php


When I try to enter my website from url www.mypage.com I got something like http://www.mypage.com/index.php/https://mypage.com/https://www.mypage.com....

When I enter from url mypage.com I got https://mypage.com - AND ITS OK

When I enter from url mypage.com/stuff I got https://mypage.com/index.php/stuff (page is working)

When I enter from url www.mypage.com/stuff I got https://mypage.com/index.php/stuff

What I should do? I would like to have always url like https://mypage.com without index.php and without 'www'.

My .htaccess :

AddHandler application/x-httpd-php70 php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Thanks!


Solution

  • Try switching the order around so the internal redirect to index.php/$1 executes last:

    AddHandler application/x-httpd-php70 php
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]