Cristina Anghel
Cristina Anghel

Reputation: 29

SSL and remove extension .html via HTACCESS without redirect chains

I see that all these scripts for removing .html extension seem not to work via SSL. I solved the issue with new script but now i am creating a 2 step redirect chain which i do not like for SEO reasons - and with your help :) i hope to get it back to a redirect step / hop.

My script is

RewriteBase /


RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.carpro.ro/$1 [R=301,L]

# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


# Remove .html extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.*)\.html$ /$1 [L,R=301]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

but it seems to create a cascade of redirects.

Is there any better version which

  1. 301 http to https for all pages of the site
  2. redirect non WWW to WWW
  3. remove .html extension and do not leave a trailing /

The script above works but with redirect cascades which i do not like

http://www.domain.com/something.html does a 301 Redirect https://www.domain.com/something.html then again a 301 Redirect https://www.domain.com/something

rather than http://www.domain.com/something.html a single redirect to https://www.domain.com/something

Any ideas on optimising this?

Upvotes: 1

Views: 649

Answers (2)

anubhava
anubhava

Reputation: 785098

Try this code:

RewriteEngine On
RewriteBase /

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^ https://www.carpro.ro%{REQUEST_URI} [NE,R=302,L]

# Force WWW prefix
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=302,L]

# Remove .html extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.+?)\.html$ /$1 [L,R=302,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+?)/?$ $1.html [NC,L]

Upvotes: 3

serverliving.com
serverliving.com

Reputation: 457

You can try below Rewrite Rules to eliminate html extension

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

And the following rewrite rule for trailing / removal

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

Hope this helps

Upvotes: 0

Related Questions