• Hi everybody, my situation is this:

    I have a WP-installation inside a subfolder, so when a visitor writes mysite.com/myfolder into browser it gets redirected to mysite.com/myfolder/

    This creates one 301-redirect, but I assume that WP makes this automatically, and it can’t be avoided. Am I incorrect?

    However, now I have migrated to https, and I use this in my htaccess-file to force it.

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

    I have also changed http to https in WP-settings.

    Everything works but the problem is that it creates two hops:

    from
    https://mysite.com/folder
    to
    https://mysite.com/folder/
    to
    https://mysite.com/folder/

    How could I reduce one hop, so https://mysite.com/folder would take directly to
    https://mysite.com/folder/

    Is there a way to achieve this, and would it create any problems?

    Ted

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes, WP does a redirect when the trailing slash/ is missing. If someone knows where this occurs, perhaps it can be dealt with along with https. Not knowing where this occurs, I suggest looking at an .htaccess solution to this.

    Add some rewrite rules that add the trailing slash if it’s missing. Relative rewrites (without http and domain) do not send redirect headers, so it’s not seen as a “hop”

    Thread Starter TT74

    (@tt74)

    Thank you @bcworkz for your reply.

    Since ‘trailing slash adding’ seems to be a built in function in WP, I’m afraid there is no htaccess-magic in the world that could override it. It probably should be changed inside the function. Unfortunately I’m not skillful enough to do that.

    Ted

    Moderator bcworkz

    (@bcworkz)

    Heh, you’re right of course. It slipped my mind how WP gets its requested URL. I did figure out how to prevent the trailing slash redirect though. I’m unsure what downside may exist in doing so, but there seems to be no ill effect in limited testing.

    add_filter( 'redirect_canonical', 'tt_can_redirect', 10, 2);
    function tt_can_redirect($redirect_url, $requested_url) {
       if ( $requested_url . '/' == $redirect_url ) return $requested_url;
       else return $redirect_url;
    }

    The passed URLs include the protocol, so the filter could be used to switch to https, except it doesn’t fire for all requests, so there’s probably little point in doing so.

    Thread Starter TT74

    (@tt74)

    Thank you for the suggestion. I will try to do some tests with that.

    I think that getting this feature changed would be beneficial to many users since more and more sites are moving towards https. So I took the initiative, and sent my suggestion for evaluation:
    https://www.ads-software.com/ideas/topic/redirecting-trailing-slash-301-directly-to-https-if-it-exists

    If you agree, please give it a vote.

    Many thanks for your help.

    Ted

    Thread Starter TT74

    (@tt74)

    Shall we @ipstenu continue this: https://www.ads-software.com/ideas/topic/redirecting-trailing-slash-301-directly-to-https-if-it-exists here.

    Thank you for taking interest in my issue. I do appreciate your time.

    Yes, the 301 is in the .htaccess file, located in the same subfolder as the WordPress install.
    https://pastebin.com/EUNGVLak

    Everything works, but .htaccess is kind of a copy paste woodoo to me. I would like to become better at it, though.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    So the way you want to do this is order things properly:

    1) Security
    2) Force HTTPS
    3) Spam & Hotlinks (minor security)
    4) Caching (WP Rocket)
    5) Any random redirects
    6) WordPress

    If you move WP rocket to between Security Hardening and WordPress, you should be okay. (And you don’t need to @ me all the time, I’m watching)

    Thread Starter TT74

    (@tt74)

    I have changed the order, and cleared WP Rocket’s cache. I’m sorry to say, but this had no effect on the redirects.

    Should the
    AuthName
    AuthUserFile
    remain after #END WordPress ?

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    So I did a check on mine:

    
    Status Code	URL	IP	Page Type	Redirect Type	Redirect URL	
    307	https://ipstenu.org/posts	(not available)	server_redirect	temporary	https://ipstenu.org/posts
    301	https://ipstenu.org/posts	50.28.57.253	server_redirect	permanent	https://ipstenu.org/posts/
    200	https://ipstenu.org/posts/	50.28.57.253	normal	none	none
    

    That’s what I expect to see. http -> https -> trailing slash.

    This has to be the order of your redirects. When done properly in the right order, WordPress (who does the adding slash) goes LAST, not first, which is what you’re seeing.

    This MAY be due to WP Rocket (don’t use it and I’m not super familiar with it’s htaccess configurations). Have you asked them?

    Thread Starter TT74

    (@tt74)

    I have sent WP Rocket a ticket and I will report back with their thougts.

    Your status check got me wondering though. If that is what you expected to see, then is my original idea still valid or am I talking just nonsense? Could WordPress be changed in a way, that https://site.com/posts would jump directly to https://site.com/posts/ ? If I test your site with Pingdom it shows me two 301’s.

    And, if my redirection chain can’t be easily fixed, is it less secure than your way?

    Thread Starter TT74

    (@tt74)

    UPDATE: WP Rocket sent me a filter for testing:
    add_filter('rocket_htaccess_mod_rewrite', '__return_false');
    It had no effect.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Notice how mine goes http -> https -> https with slash?

    Yours is http -> http with slash -> https with slash

    It’s not BAD, it’s just very odd that it’s backwards. That middle one should be https.

    You’re going to get three hops, though, if you go to http without a slash, since the code to https properly can’t include the slash addition (it doesn’t know). You could probably make a more complex rule though.

    If you really want to debug this, strip the htaccess down to JUST the https and the NORMAL WordPress call, and watch what happens. I use https://chrome.google.com/webstore/detail/redirect-path/aomidfkchockcldhbkggjokdkkebmdll?hl=en most of the time for that.

    Thread Starter TT74

    (@tt74)

    Oh well, if it’s not bad for security or for SEO, I might just let it be.

    I feel I have taken enough of your time already. I thank you kindly for your time, and for letting me to pick your brain.

    If I do continue to debug this, and find the solution, I will post my findings here.

    Anyways, I have sent you some coffee ??

    Thank you

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘How can I minimize domain redirect chain’ is closed to new replies.