• Resolved George

    (@giorgos93)


    Hi, I use 301 redirect option (through .htaccess) in Really Simple SSL plugin. However, if I type https://www.litfan.ru/ , then appears a double redirect (first one from .htacess , and second one from WordPress php). Is there any way how to redirect from www to non www URL on .htaccess level only? Do you have this option in your plugin’s settings? Atm I found this code on Internet for .htaccess:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.litfan\.ru [NC]
    RewriteRule ^(.*)$ https://litfan.ru/$1 [L,R=301]
    </IfModule>

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Support Jarno Vos

    (@jarnovos)

    Hi @giorgos93,

    The 301 .htaccess redirect in this plugin would only redirect the user from http:// to https://, but it does not add or remove the “www.” part from the URL.

    As far as I can see, this already seems to be working as expected when visiting your site, as the redirect chain is: http://www.litfan.ru/ -> https://www.litfan.ru/ -> https://litfan.ru/, which is considered a best practice (first we redirect from http:// to https:// and remove www. subdomain afterwards).

    This redirect should be the one that originates from your .htaccess file as opposed to the WordPress redirect, as this exact same redirect still takes place when I reach your Bot Prevention/CAPTCHA page (outside of WordPress), and as WordPress isn’t loaded at that point, it can’t use the PHP redirect either.

    Kind regards, Jarno

    Thread Starter George

    (@giorgos93)

    Hi, @jarnovos

    Yes, it works, because I’ve added the code yesterday, that I mentioned above. That’s great, that it works in such order, even though I added the code before your plugin’s code in .htacess. Why did the website make a right order then?

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.litfan\.ru [NC]
    RewriteRule ^(.*)$ https://litfan.ru/$1 [L,R=301]
    </IfModule>
    
    #Begin Really Simple Security
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{REQUEST_URI} !wp-content\/cache\/(all|wpfc-mobile-cache)
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
    </IfModule>
    
    #End Really Simple Security

    And also: the removal of www. is working on .htaccess only? WP doesn’t trigger it again on php-level? It would be bad, if the same redirect is triggering twice.

    Plugin Support Jarno Vos

    (@jarnovos)

    Hi @giorgos93,

    I hope I understand your question correctly; the rewrite rules in are processed from top to bottom, but as your redirect uses the [L] flag, it stops any further rewrite processing if the condition is met.

    That’s why everything still works as expected, your rule handles the www. redirect, and our rules below that would only take effect if the request doesn’t match yours.

    And indeed, WordPress itself wouldn’t trigger the same redirect again in PHP. If the redirect is already completed in the .htaccess, WordPress would receive the request directly at the correct (non-www) URL.

    Kind regards, Jarno

    Thread Starter George

    (@giorgos93)

    Hi, @jarnovos

    Yes, you did understand me correctly. And yes, the rewrite rules are processed from top to bottom in .htaccess. That is why I was surprised, that they work in a right order, because www removal starts before https redirect in .htaccess. But you wrote, that the redirect chain is: http://www.litfan.ru/ -> https://www.litfan.ru/ -> https://litfan.ru/ . But it should be vice versa, because of my code’s order. Shouldn’t it?

    Plugin Support Jarno Vos

    (@jarnovos)

    Hi @giorgos93,

    Yes, I think you are correct. It was bit more challenging to “see” the actual redirects here as I can’t do a check via cURL (as I get the verification screen instead).

    But indeed on closer inspection with a request to http://www.litfan.ru, it looks like your www removal rule matches immediately and therefore issues a redirect to https:// in one step.

    Kind regards, Jarno

    Thread Starter George

    (@giorgos93)

    @jarnovos is it a normal behaviour? It should be like that in my case, or I need to change something?

    Plugin Support Jarno Vos

    (@jarnovos)

    Hi @giorgos93,

    Yes, your redirect is getting triggered exactly as expected. This is perfectly fine to handle both the www. removal and enforcing HTTPS.

    Kind regards, Jarno

    Thread Starter George

    (@giorgos93)

    Thanks @jarnovos! Is it possible to add such option in your plugin in future, so there is no need to change the code manually? It would be useful for websites, that want to redirect from www on .htaccess level.

    Thread Starter George

    (@giorgos93)

    @jarnovos and one more question: I use WP Fastest Cache plugin on my website, and Really Simple Security has this piece of code in my .htaccess file:

    RewriteCond %{REQUEST_URI} !wp-content\/cache\/(all|wpfc-mobile-cache)

    Is it better to add this piece of code in my version of code as well? So it’ll look like this:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.litfan\.ru [NC]
    RewriteCond %{REQUEST_URI} !wp-content\/cache\/(all|wpfc-mobile-cache)
    RewriteRule ^(.*)$ https://litfan.ru/$1 [L,R=301]
    </IfModule>
    Plugin Support Jarno Vos

    (@jarnovos)

    Hi @giorgos93,

    While I do understand the request, we currently focus strictly on the protocol (e.g., http:// -> https://), without enforcing a specific domain format like www. or non-www, and I suspect that this approach is unlikely to change in the near future.

    As for your other question, the condition is used in the HTTPS redirect to avoid unnecessary redirects on cached files (/wp-content/cache/). Your redirect is unlikely to interfere with those, but it also won’t hurt to add it anyway.

    Kind regards, Jarno

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Double redirect from https://www’ is closed to new replies.