• Resolved jwamsterdam

    (@jwamsterdam)


    Hi,

    I would like to add the following redirections to my website:
    – add trailing slash
    – redirect non-www to www
    – redirect http to https

    In my site admin panel, I have configured the site url to https://www. and at this moment the http is redirected to https. I don’t see this rule in htaccess, so I guess this might be redirected by wordpress or google chrome.

    I would like to add the following rules:

    #### Force HTTPS://WWW and remove trailing / from files ####
    ## Turn on rewrite engine
    RewriteEngine on
    
    # Remove trailing slash from non-filepath urls
    RewriteCond %{REQUEST_URI} /(.+)/$
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ https://www.example.com/%1 [R=301,L]
    
    # Include trailing slash on directory 
    RewriteCond %{REQUEST_URI} !(.+)/$
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.+)$ https://www.example.com/$1/ [R=301,L]
    
    # Force HTTPS and WWW 
    RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
    RewriteCond %{https} off  
    RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
    
    source: https://www.danielmorell.com/guides/htaccess-seo/redirects/https-www-and-trailing-slash

    looking at my htaccess, I see the following rewrite conditions at the top of my htaccess file:

    # BEGIN WordPress
    # De richtlijnen (regels) tussen <code>BEGIN WordPress</code> and <code>END WordPress</code> worden
    # dynamisch aangemaakt en zouden enkel aangepast mogen worden via WordPress filters.
    # Elke wijziging aan deze richtlijnen tussen deze markeringen worden overschreven.
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

    as well as the following code from cache-enaber:

    # BEGIN Cache Enabler
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
    
        # set blog sub path
        SetEnvIf Request_URI "^(.*)$" SUB_PATH=/wp-content/cache/cache-enabler/
    
        # set Cache Enabler path
        SetEnvIf Request_URI "^(.*)$" CE_PATH=$1
        SetEnvIf Request_URI "^(/)index.php$" CE_PATH=$1
    
        <IfModule mod_mime.c>
            # webp HTML file
            RewriteCond %{ENV:CE_PATH} /$
            RewriteCond %{ENV:CE_PATH} !^/wp-admin/.*
            RewriteCond %{REQUEST_METHOD} !=POST
            RewriteCond %{QUERY_STRING} =""
            RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
            RewriteCond %{HTTP:Accept-Encoding} gzip
            RewriteCond %{HTTP:Accept} image/webp
            RewriteCond %{DOCUMENT_ROOT}%{ENV:SUB_PATH}%{HTTP_HOST}%{ENV:CE_PATH}index-webp.html.gz -f
            RewriteRule ^(.*) %{ENV:SUB_PATH}%{HTTP_HOST}%{ENV:CE_PATH}index-webp.html.gz [L]
    
            # gzip HTML file
            RewriteCond %{ENV:CE_PATH} /$
            RewriteCond %{ENV:CE_PATH} !^/wp-admin/.*
            RewriteCond %{REQUEST_METHOD} !=POST
            RewriteCond %{QUERY_STRING} =""
            RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
            RewriteCond %{HTTP:Accept-Encoding} gzip
            RewriteCond %{DOCUMENT_ROOT}%{ENV:SUB_PATH}%{HTTP_HOST}%{ENV:CE_PATH}index.html.gz -f
            RewriteRule ^(.*) %{ENV:SUB_PATH}%{HTTP_HOST}%{ENV:CE_PATH}index.html.gz [L]
    
            AddType text/html .gz
            AddEncoding gzip .gz
        </IfModule>
    
        # webp HTML file
        RewriteCond %{ENV:CE_PATH} /$
        RewriteCond %{ENV:CE_PATH} !^/wp-admin/.*
        RewriteCond %{REQUEST_METHOD} !=POST
        RewriteCond %{QUERY_STRING} =""
        RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
        RewriteCond %{HTTP:Accept} image/webp
        RewriteCond %{DOCUMENT_ROOT}%{ENV:SUB_PATH}%{HTTP_HOST}%{ENV:CE_PATH}index-webp.html -f
        RewriteRule ^(.*) %{ENV:SUB_PATH}%{HTTP_HOST}%{ENV:CE_PATH}index-webp.html [L]
    
        # default HTML file
        RewriteCond %{ENV:CE_PATH} /$
        RewriteCond %{ENV:CE_PATH} !^/wp-admin/.*
        RewriteCond %{REQUEST_METHOD} !=POST
        RewriteCond %{QUERY_STRING} =""
        RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
        RewriteCond %{DOCUMENT_ROOT}%{ENV:SUB_PATH}%{HTTP_HOST}%{ENV:CE_PATH}index.html -f
        RewriteRule ^(.*) %{ENV:SUB_PATH}%{HTTP_HOST}%{ENV:CE_PATH}index.html [L]
    
        # wp override
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [END]
    </IfModule>
    # END Cache Enabler

    Now I think I have duplicate rewrite rules, which can / should be simplified.

    But besides that, by adding the rules I have posted at the beginning of this message to the top of my htaccess, I get an error saying: too many redirects.

    Could you help me out setting up the correct htaccess rules, while keep using the htaccess static page functionality of cache-enabler?

    Thank you!

    • This topic was modified 6 years, 6 months ago by jwamsterdam.
    • This topic was modified 6 years, 6 months ago by jwamsterdam.
    • This topic was modified 6 years, 6 months ago by jwamsterdam.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘htaccess redirection’ is closed to new replies.