Welcome to WebmasterWorld Guest from 3.227.233.55

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Multiple 301 redirects to a page

     
9:25 am on Jul 12, 2015 (gmt 0)

Full Member

10+ Year Member Top Contributors Of The Month

joined:June 3, 2005
posts:298
votes: 12


Presuming there is a loss of around 15% page rank per 301 redirect what I want to know is that if a htacess file contains 2 rules a URL has to go through eg:

1) The page gets redirected from http to https
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [R,L]
</IfModule>

2) The same page then gets redirected from old file name to new file name
Redirect 301 /directors.htm /category/directors


All on the same server. Has the URL (http domain/directors.htm) gone through two 301 redirects or is the server clever enough to do one 301 URL redirect?
5:33 pm on July 12, 2015 (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:15804
votes: 845


Get rid of all mod_alias (Redirect by that name) rules. mod_rewrite executes before mod_alias, so all redirects should be handled in mod_rewrite. There's absolutely no reason to use both, and there can be unintended consequences.

If you have a lot of existing mod_alias redirects, there are some find-and-replaces you can run on your htaccess text file to convert all the rules globally.

is the server clever enoug

A redirect is, by definition, a message sent back to the user; that's one difference between a redirect and a rewrite. Once a redirect has been issued, it can't be canceled by any other redirect. (A redirect can be overridden by a 403, but that's a different issue.)
6:18 pm on July 12, 2015 (gmt 0)

Senior Member from GB 

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

joined:Apr 30, 2008
posts:2630
votes: 191


is the server clever enough to do one 301 URL redirect?

As Lucy said above:
Once a redirect has been issued, it can't be canceled by any other redirect.

However, what can be controlled is WHEN a redirect is issued. Consider this:

# ORDER OF DIRECTIVES FOR .htaccess
# Access controls first (e.g. deny/alow, IP filtering)
# Redirects next. Within the list of redirects, most specific first, most general last
# Rewrites last. Within the list of rewrites, most specific first, most general last
# errordocument after rewrites
# Most specific means affects the smallest number of URL requests
# Most general means affects the largest number of URL requests

Now, by having your redirect from /directors.htm to https://www.example.com/category/directors before your catch-all redirect (and using mod_rewrite syntax), then this redirect will execute first and will go to target location "in one hit", therefore avoiding redirect chain.

7:21 pm on July 12, 2015 (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:15804
votes: 845


# errordocument after rewrites

What does this mean? I can think of two things associated with error documents. One is the ErrorDocument directive, which is independent of mod_rewrite so it doesn't matter where it goes. The other is protection for error documents, like
RewriteRule my-custom-403\.html - [L]
to prevent infinite loops. Seems like this would have to go before all other RewriteRules, particularly the ones concerned with access control. (Once on a test site I blundered somewhat appallingly on this point: I hadn't bothered to code an error document override, but did have a www redirect, so all my blocked visitors who'd asked for the wrong hostname turned into explicit requests for "forbidden.html". It took me an embarrassingly long time to figure out why it was happening. Luckily it was only a test site.)
10:14 pm on July 12, 2015 (gmt 0)

Senior Member from GB 

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

joined:Apr 30, 2008
posts:2630
votes: 191


ErrorDocument directive
Yes, this one.

which is independent of mod_rewrite
Learn something every day :)

But importantly for this thread - the OP should re-order their redirect rules in .htaccess and in that way they could avoid double redirects.
10:51 am on July 13, 2015 (gmt 0)

Full Member

10+ Year Member Top Contributors Of The Month

joined:June 3, 2005
posts:298
votes: 12


The information and links you have all provided me is excellent and I thank you both very much.

I plan to write something like this when I get home tonight (though it will be larger with 2000 legacy query string URL's):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteBase /

RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ https://www.domain.com/$1 [R=301,L]

RewriteRule ^/directors.html$ https://www.domain.com/directors [R=301,L]

RewriteCond %{QUERY_STRING} ID=1$
RewriteRule ^review\.asp$ https://www.domain.com/reviews/scarface? [R=301,L]

RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
</IfModule>


If somebody could please proof read the above or know of a more optimal way to write what I have done please do post. I will be using aakk9999 ordering of .htaccess file around the above code.
8:17 pm on July 13, 2015 (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:15804
votes: 845


RewriteCond %{SERVER_PORT} 80

What RewriteRule does this condition belong to? I don't know offhand which of these three will happen:
-- condition is ignored because it isn't immediately followed by another condition or rule
-- 500-class error because condition comes before RewriteBase directive
-- condition is taken as part of the next RewriteRule, wherever it may be

I do know that when I copy-and-pasted this line there was a trailing space. I hope that isn't part of your real htaccess file, because that will result in a 500 error. 8,000 guesses how I know this.

As already noted, the index redirect and the domain-name canonicalization redirect (which, incidentally, seems to be missing a Condition, assuming you don't want an infinite loop-- is it possible the RewriteCond from the beginning got misplaced?) should be the last two. If you're redirecting everything to https, this can be a single rule with a pair of [OR] delimited conditions: If domain name isn't exactly right, OR if protocol is wrong.

You do not need the <IfModule> envelope. Either you've got mod_rewrite or you haven't.

The locution
^.*/
is never needed. Just leave off both the anchor and the .*

I think there must have been two near-identical questions in the Apache subforum, because I was about to start muttering that I told you to add the [NS] flag, but, er, I guess that was the other guy. Use [NS] in ordinary index redirects so the server doesn't have to evaluate conditions. In fact, if it's a vanilla directory-index belonging to a directory that physically exists, you don't need a RewriteCond at all, just the flag. [NS] here means "ignore requests from mod_dir doing its job".

Learn something every day

There are not many nice things to say about mod_rewrite ;) but one of them is: all of its directives begin with the element RewriteBlahblah. I don't think there are any exceptions.
1:51 pm on July 14, 2015 (gmt 0)

Full Member

10+ Year Member Top Contributors Of The Month

joined:June 3, 2005
posts:298
votes: 12


RewriteCond %{SERVER_PORT} 80

Somebody told me this would speed up WordPress but having done some tests now I have removed the line.

The trailing white space is interesting as I have lots of them. I have used an online tool to remove them and I was more able to make more sweeping changes to my file without a 500 error message.

Perhaps due to my lack of understanding I am not sure about using [NS] as the query string ID value are uppercase as the site was originally windows.
8:22 pm on July 14, 2015 (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:15804
votes: 845


I am not sure about using [NS] as the query string ID value are uppercase as the site was originally windows.

The [NS] flag is "no subrequests". You may be thinking of the wholly unrelated [NC].

If you're redirecting from http to https, then {SERVER_PORT} is one way to do it. Another is {HTTPS} (with values "on"/"off"}. I think {SERVER_PROTOCOL} will also do it. But the condition has to be attached to the appropriate rule; there's no such thing as a global condition covering multiple rules. (Possibly in 2.4 using an <If...> envelope, but this can get messy.)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members