2

I am looking at shaving www. and enforcing https on the entire site without a plugin. All along removing the index.php.

Here is my .htaccess file, it works but is it optimized. If you have any suggestions on how to condense or streamline would like to hear or see an example please. Thanks!

<IfModule mod_rewrite.c>

RewriteEngine On

# Remove WWW and SSL enforce
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]

</IfModule>

2 Answers 2

5

Been using this one for years, works great:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # ========================================= #
    # Force website to use "www"
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
    # ========================================= #

    # ========================================= #
    # Force website to use "https"
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
    # ========================================= #

    # ========================================= #
    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]
    # ========================================= #

</IfModule>

This one adds www, but it's simple to reverse that (as you do in your original example).

3

Since you mentioned in an earlier post that you are performance-minded, this might interest you: Stop using .htaccess files! No, really.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.