URLs without Trailing Slash - Kill Performance - ~5 sec per page load - htaccess help?

Hi:
Did you know the absence of a trailing slash costs about 5 seconds on page load time?
Well neither did I.... I'm running multi-site with child blogs. Links without the trailing slash take about 5 seconds extra to load from the redirect.

I can't seem to get any .htaccess fixes to work except for a simple redirect. This one works fine:
Redirect 301 /foobar /blog/pemf/

But more complex rules don't:
Here are two I've tried. Here is an article on the topic.

# Doesn't seem to work (preferred)
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9]+)/$ /$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z0-9]+)
RewriteRule ^([a-zA-Z0-9]+)$ /%1/? [R=301,L]

# And niether does this one
# If requested resource does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# and does not end with a period followed by a filetype
RewriteCond %{REQUEST_URI} !\..+$
# and does not end with a slash
RewriteCond %{REQUEST_URI} !/$
# then add a trailing slash and redirect
# This one can't work because I have other subdomain sites running too (just a test though)
RewriteRule (.*) http://whnlive.com/$1/ [R=301,L]

  • squibm
    • Site Builder, Child of Zeus

    Will try it - definitely a good idea for posts.

    Though I doubt it will work for pages and child blogs because those aren't permalinks - by my reckoning. They're just static page addresses. If I access a static page that isn't managed by permalink, you get the same delay.

    See the screen-shots below that show and extra 3.81 second delay for accessing a hosted page without the trailing slash. This cannot be fixed - although I suspect NOT making this change to permalinks would result in a similar delay for posts.

  • Timothy
    • Chief Pigeon

    Hey again

    Will try it - definitely a good idea for posts.
    Though I doubt it will work for pages and child blogs because those aren't permalinks - by my reckoning. They're just static page addresses. If I access a static page that isn't managed by permalink, you get the same delay.

    I have this on my live networks and my sandbox and it appends the slash as expected.

    My permalink structures is as above. :slight_smile:

    Let me know how you get on.

    Thanks.

  • squibm
    • Site Builder, Child of Zeus

    Hi Timothy:
    To be clear... I confirm that this is not a permalink issue. My standing permalink settings already implement your suggestion, Please see image of my perma-link settings above.

    The requirement appears to be to suffix a request name with a trailing slash to avoid invoking the redirect before calling wordpress. Invoking wordpress to calculate the redirect apparently causes the big delay. This means using wordpress to add the slash takes a very long time.

    It seems to me that this ought to be a job for .htaccess - although for the life of me I cannot figure out how to implement all but the simplest redirect.

    Here is the code closest to working:
    # Does it not end with a slash
    RewriteCond %{REQUEST_URI} !(.*)/$
    # And is not a file
    RewriteCond %{REQUEST_FILENAME} !-f
    # And is Not a Directory
    RewriteCond %{REQUEST_FILENAME} !-d
    # And ends with foo ( For Selective Debugging )
    RewriteCond %{REQUEST_URI} ^(.*)foo$
    # then Rewrite the rule with a trailing slash - it works!
    RewriteRule ^(.*)$ %{REQUEST_URI}/ [R=307,L]

    But when I attempt to get it to work for a subdirectory - like http://whnlive.com/blog/foo
    It will not work - and I cannot figure out why. This is where I need help!

    # Does it not end with a slash
    #( same conditions as above)
    # And ends with foo ( For Selective Debugging )
    RewriteCond %{REQUEST_URI} ^(.*)/blog/foo$
    # then Rewrite it with a trailing slash - Then it does not work
    RewriteRule ^(.*)$ %{REQUEST_URI}/ [R=307,L]

    What must I do to get this to work?

  • Timothy
    • Chief Pigeon

    Just so we are on the same page here. When I'm on your site if I hover over any link and click any link I see a / on the end of the link? Is this not what we are talking about?

    Just want to be sure I understand....

    I've attached screenshots, you can see in the lower left the link as I hover.

  • squibm
    • Site Builder, Child of Zeus

    Absolutely not.

    It's really easy to check here are to ways to load the same link to a simple in-site page:
    http://whnlive.com/blog/technology - 6.74 seconds / gtmetrix - 4.98 seconds
    http://whnlive.com/blog/technology/ - 2.82 seconds / gtmetrix - 1.8 seconds

    You can check this with firebug or enter each into http://gtmetrix.com
    The redirect for the version without the trailing slash can take up to 10 seconds longer because the redirect from http://whnlive.com/blog/technology to http://whnlive.com/blog/technology/
    takes a very long time.

  • Timothy
    • Chief Pigeon

    Well just out of interest when would someone being in on those links without a /?

    All your sites anchors include them, and looking at Google it sends them through with the right one.

    So I would assume that the only time they are not on is if you manually type in the URL. But after the first load then every subsequent click would be on the site thus with the /?

    When do you envisage users being on your site without the /?

    Thanks.

  • squibm
    • Site Builder, Child of Zeus

    There are lots of circumstances:

    • Whenever somebody types it - people are lazy - or don't know the difference.
    • Whenever a search engine decides not to use a trailing slash because some programmer forgot, or didn't know it made a difference
    • Whenever somebody external links to one of my pages and doesn't use a slash

    For example... Do you type a slash at the end of every URL you use? Or do you put a slash every time you link to somebody else's site?
    Ahem... I sure don't.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.