Welcome to WebmasterWorld Guest from 23.22.240.119

Forum Moderators: Ocean10000 & incrediBILL & phranque

How to avoid double redirection 301

redirect 301 htaccess

     
12:36 am on Dec 19, 2017 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 19, 2009
posts: 57
votes: 2


Hi,

I'm having problem to make a redirect because I'm getting two steps, in first step change to https and in the second redirect to the correct url.


RewriteRule ^search/(.*)$ https://search.domain.com/$1 [R=301,L] --> this doesn't work infinite looping
RewriteCond %{HTTP_HOST} !^search.domain.com$
RewriteCond %{HTTP_HOST} !^images.domain.com$
RewriteCond %{HTTP_HOST} !^css.domain.com$
RewriteCond %{HTTP_HOST} !^www.domain.com$

RewriteRule ^(.*) https://www.domain.com/$1 [L,R=301]

RewriteRule ^photos/originals/([0-9]+)_1\.jpg$ https://www.domain.com/photos/$1/$1_l.jpg [R=301,L]


When I load http://www.domain.com/originals/123_1.jpg (301 to) --> https://www.domain.com/originals/123_1.jpg (301 to) --> https://www.domain.com/photos/123/123_l.jpg
When I load http://domain.com/originals/123_1.jpg (301 to) --> https://www.domain.com/originals/123_1.jpg (301 to) --> https://www.domain.com/photos/123/123_l.jpg

Also I want to redirect www.domain.com/search/1234/several-words to search.domain.com/1234/several-words with the first instruction but I get infinite looping.

Really what I want is to include different redirections for every subdomain and by defect load www if don't have it, but only in one step but I don't know how to do it.

Thank you,
Carlos

[edited by: phranque at 12:43 am (utc) on Dec 19, 2017]
[edit reason] unlinked urls [/edit]

12:54 am on Dec 19, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11144
votes: 116


the first thing you should do is reorder the rulesets so the hostname canonicalization redirect is last. (you have it as the second ruleset)
i would also add a condition to this rule so that it fires when either the hostname is noncanonical or it's a non-https request.

this doesn't work infinite looping

could you give an example of the redirect series in this infinite loop?

https://www.domain.com/originals/123_1.jpg (301 to) --> https://www.domain.com/photos/123/123_l.jpg

i don't see a rule that would redirect /originals/ to /photos/
1:32 am on Dec 19, 2017 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 19, 2009
posts: 57
votes: 2


Hi phranque,

if I load https://www.domain.com/search/1244/several-words redirect continuosly to https://search.domain.com/1244/several-words although it is the correct url.

The last rule (RewriteRule ^photos/originals/([0-9]+)_1\.jpg$ https://www.domain.com/photos/$1/$1_l.jpg [R=301,L]) make that redirect.

How can I check if is noncanonical or it's a non-https request?

Thank you

[edited by: phranque at 3:02 am (utc) on Dec 19, 2017]
[edit reason] unlinked urls [/edit]

2:08 am on Dec 19, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14383
votes: 567


i don't see a rule that would redirect /originals/ to /photos/

It's this one, isn't it?
RewriteRule ^photos/originals/([0-9]+)_1\.jpg$ https://www.domain.com/photos/$1/$1_l.jpg [R=301,L]
It redirects /photos/originals/blahblah to /photos/modified-blahblah/blahblah with a newly added subdirectory replacing /originals/

Please take a minute to absorb the rule about using example.com (or example.any-old-tld if you need more than one). There's more than one reason for this rule.
RewriteCond %{HTTP_HOST} !^search.example.com$
RewriteCond %{HTTP_HOST} !^images.example.com$
RewriteCond %{HTTP_HOST} !^css.example.com$
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*) https://www.example.com/$1 [L,R=301]
This can all be collapsed to
RewriteCond %{HTTP_HOST} !^(search|images|css|www)\.example\.com$
RewriteRule ^(.*) https://www.example.com/$1 [L,R=301]
... except I don't think this rule will do exactly what you want it to. You need two rules: one for (search|css|images) non-HTTPS to HTTPS, and another one for all other incorrect host and/or protocol to https://www.example.com/etcetera.

Get rid of the blank line between Conditions and Rule. It has no effect on the server (unlike, say, blank lines in robots.txt which do have syntactic meaning), but it does create confusion in the human reader.
3:10 am on Dec 19, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11144
votes: 116


i don't see a rule that would redirect /originals/ to /photos/

It's this one, isn't it?
RewriteRule ^photos/originals/([0-9]+)_1\.jpg$ https://www.domain.com/photos/$1/$1_l.jpg [R=301,L]
It redirects /photos/originals/blahblah to /photos/modified-blahblah/blahblah with a newly added subdirectory replacing /originals/

this rule redirects ^photos/originals/blahblah to /photos/modified-blahblah/blahblah but it won't match this request:
http://www.domain.com/originals/123_1.jpg 

(i.e. it doesn't match ^originals/blahblah)

[edited by: phranque at 4:52 am (utc) on Dec 19, 2017]

3:26 am on Dec 19, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11144
votes: 116


if I load https://www.domain.com/search/1244/several-words redirect continuosly to https://search.domain.com/1244/several-words although it is the correct url.

you must add RewriteCond directives to this ruleset to exclude the rule if the protocol and hostname are already canonical (i.e. search.domain.com [AND] HTTPS is on)

How can I check if is noncanonical or it's a non-https request?

after "everything else" (meaning the more specific redirect rulesets) you will need something like this:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

with the RewriteCond modified to exclude all your canonical hostnames from the redirect to avoid chained redirects, perhaps similar to lucy24's suggestion.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members