Forum Moderators: phranque

Message Too Old, No Replies

Resolving Redirect Issues with Different URL Versions

         

PawkyPuck

2:47 pm on May 11, 2020 (gmt 0)

5+ Year Member



Hi all,
Our website has undergone both a redesign (with new URLs) and a migration to HTTPS in recent years. I'm having difficulties ensuring all URLs redirect to the correct version all the while preventing redirect chains. Right now everything is redirecting to the correct version but it usually takes up to two redirects to make this happen. See below for an example. How do I go about addressing this, or is this not even something I should concern myself with?
Redirects (2)
Redirect Type
URL

http://www.example.com/blog/2009/index.html
301
https://example.com/blog/2009/index.html
301
https://example.com/blog/

This code below was what we added to our htaccess file. Prior to adding this, the various subdomain versions (www, non-www, http, etc.) were not redirecting properly. But ever since we added it, it's now created these additional URLs (see bolded URL above) as a middle step before resolving to the correct URL.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Your feedback is much appreciated. Thanks in advance for your help.

Sincerely, Puck


[edited by: not2easy at 3:09 pm (utc) on May 11, 2020]
[edit reason] exemplified and formatted for readability [/edit]

not2easy

3:40 pm on May 11, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Hi PawkyPuck and Welcome to WebmasterWorld [webmasterworld.com]

I apologize for the edits but we use example.com to replace domain names so that URLs don't auto-link and become unreadable. The addition of code tags makes your code readable. Tips on using forum features are in that Welcome link ;)

Your problem can be easily remedied by using a single rule for the https and canonicalization RewriteRules you have there. Part of the problem is that both rules have the "L" flag which tells the server not to process more rules - but without processing the next rule you're not sending traffic to the https version of your domain. The actual domain name should always be used in the Rule's target in place of "%{HTTP_HOST}%" just as best practice.

Our local Search can help you find related threads - there are thousands on this topic. This one may help you pick up on how it's done: [webmasterworld.com...]

If you are wondering why this isn't a more specific answer, please read the Apache Web Server forum Charter [webmasterworld.com] - We're happy to help but copy/paste answers don't really ensure that you're getting the answer you need. Not all situations are the same, and even pasting the right answer in the wrong position may not solve your problem. We try to help you understand both what and why ;)

lucy24

4:27 pm on May 11, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



#1 Instead of two separate rules, put it into a single rule with [OR]-delimited conditions.

#2
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
The condition should be expressed as a negative, meaning “anything other than this one specific form”: depending on site, either
RewriteCond %{HTTP_HOST} !^example\.com$
or
RewriteCond %{HTTP_HOST} !^(example\.com)?$
On a shared server using a VirtualHost, you don’t need the (blahblah)? element, but it does no harm and makes some people feel safer. You can also argue about the [NC]; human browsers tend to flatten casing of hostnames, so the only requests for EXAMPLE.COM are from robots that will be blocked anyway.

Wherever possible, use your actual site name in all rules. The exception is if you’ve got dozens of sites all sharing the same RewriteRules, which doesn’t seem to be the case here.

PawkyPuck

3:40 pm on May 14, 2020 (gmt 0)

5+ Year Member



Thanks, not2easy & lucy24. I'll give these options a try. I appreciate your help!

phranque

12:44 am on May 15, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, PawkyPuck!