Forum Moderators: phranque

Message Too Old, No Replies

Re: Redirection with htaccess

         

Leweez

6:18 pm on Mar 3, 2015 (gmt 0)

10+ Year Member



.htaccess is fairly new so bare with me if this question might seem stupid for some.

I have a new domain with a new host provider (www.example.net).

My old domain from another host provider will still be active for a while (www.example.com).

I want to make redirects for my customers and not sure if it's wise to use my .htaccess for that.

The thing is, my old domain has been under several attacks lately and I don't want to redirect those hackers and spammers to my new domain. These crooks are blocked with my htaccess file for now and they all get a 403 Forbidden code for the moment.

If the "redirection" to the new domain is placed at the bottom of my htaccess file, will my "hackers/spammers" blocking commands will do the job BEFORE it gets to the redirection?

Hope I explained well enough my question?

Thank in advance for your help!
Regards
Leweez

lucy24

8:27 pm on Mar 3, 2015 (gmt 0)

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



You're asking about which command, or which module, executes first. Happily this is an answerable question :)

#1 Each module is an island. Within any given module, commands are executed in sequential order. But you have no control over the order of modules: mod_setenvif before mod_rewrite before mod_alias before mod_authzthingy, for example. Each module, in turn, looks only at its own directives, ignoring other mods' directives even if they are all mixed up together.

In a typical htaccess, you might have a lot of rules using mod_authzzzz "Deny from suchandsuch" and then other rules creating redirects with mod_alias or mod_rewrite (Redirect by that name, or RewriteRule). It doesn't matter what order you put these groups in, because each module is separate.

#2 A response header isn't sent out until all modules have had their crack at a given request. If any module generates a 403 (F, lockout) this will overrule any other response generated by any other module, whether earlier or later.

In short: Arrange your htaccess in the order that is most convenient for you to read and edit. 403 lockouts will be issued no matter where the rule is located.


Your underlying concern is a good general principle. There's no point in redirecting someone if they're going to end up being [F] 403 locked out. But this only applies to rule ordering within any one module (see multiple threads in this forum about the best ordering of RewriteRules).

Keep all your existing rules that create a 403. Just add the redirect at the end.

For humans, you only need to redirect pages; once they're on the new site, they'll request images and stylesheets from there. But you'll need a global redirect because search engines ask for non-page files "cold".

If you have any intervening rules such as index redirects (directory/index.html >> directory/ alone), keep the rules but change the target so requests are redirected straight to the new URL. Same goes for any page-specific redirects that the old domain has picked up over the years.

If you've already got a domain-name-canonicalization (with/without www) redirect using mod_rewrite, it will be replaced with the new example.com redirect. So delete the RewriteCond from this rule; you're now redirecting everyone, regardless of what they initially requested.


All this is assuming that requests for the old site and new site do not pass through the same htaccess. If they're on the same server, some things will be a little different.

Leweez

12:46 am on Mar 4, 2015 (gmt 0)

10+ Year Member



True that the old site and new site won't have the same htaccess and they are not on the same server.

Thank you so much for your help.
I understand more and more how htaccess works!

Regards
Leweez

phranque

1:49 am on Mar 4, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, Leweez!


you should carefully study jdmorgan's response in this thread - Proper Order for htaccess:
http://www.webmasterworld.com/apache/4184253.htm?highlight=msg4187743 [webmasterworld.com]