Fatih Donmez
Fatih Donmez

Reputation: 4347

url rewriting to redirect www to non www on https

I just changed my website http to https. I'll already redirected http to https version. I also want to redirect www version to non www. So far couldn't succeed it.

http://www.domain.com => https://domain.com

https://www.domain.com => https://domain.com

Here is the .htaccess file;

<ifModule mod_rewrite.c>
    RewriteEngine On
        redirect 301 /makale /sozluk
        RewriteCond %{REQUEST_FILENAME} !-s
        RewriteRule ^(.*)\.[\d]+\.(css|js)$ $1.$2 [L]

        RewriteCond %{HTTPS ^www.sporapp.com$ [NC]
        RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]

        RewriteCond %{HTTPS} !=on
        # This checks to make sure the connection is not already HTTPS
        RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
</ifModule>

Thank you.

Upvotes: 0

Views: 250

Answers (1)

Jon Lin
Jon Lin

Reputation: 143956

You should remove this bit:

RewriteCond %{HTTPS ^www.sporapp.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

And add this just under the RewriteEngine On:

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

You need to have the redirecting happening before you rewrite, otherwise the request gets changed before it gets redirected.

Also, you have mod_alias directives which will interfere with the rewrite to /index.php:

redirect 301 /makale /sozluk

Should be:

RewriteRule ^makale/(.*)$ /sozluk/$1 [L,R=301]

Upvotes: 1

Related Questions