2

We are using Apache server with a .config file to do permanent 301 redirects.

The web site is mostly maintained and content is provided by the client. This particular web site changes very often, and so I am frequently asked to add to the 301 redirects as the pages change.

This has resulted in multiple redirects for the same URLs as below:

Redirect 301 "/foo/old" "/foo/new"
Redirect 301 "/foo/old" "/foo/bar/new"
Redirect 301 "/foo/old" "/new/foo"

Which one of these redirects will apache use? Configtest tells me that the syntax is OK.

  • I am unable to comment because i don't have a reputation :-) However, your answers and comments have been most helpful, thank you. We are, for performance reasons, not using mod_rewrite or htaccess files, but a redirect.conf file in the root. I checked this yesterday using a url that I know is duplicated. Weirdly, it seems to process the last rule, not the first. Maybe a better way of putting it is that it somehow redirects to the valid URL. I should add that the "older" urls are often no longer valid. – timlza Feb 3 '18 at 8:03
2

Which one of these redirects will apache use?

Apache will process the .Htaccess file from top to bottom. When a condition is met, it processes the condition. When done, Apache will start at the top and process all the rules again to ensure that there are no more matching conditions.

Using your example, once the first rule

Redirect 301 "/foo/old" "/foo/new"

is matched against the condition "/foo/old" it processes the rule and starts again. However, the request is now "/foo/new" and does not match any condition defined within the rules. Therefore only the first rule in your example can be processed. You should only have one rule per condition.

  • I wasn't aware that mod_redirect does multiple passes to match rules. Unlike mod_rewrite it doesn't internally modify the URL, so I don't see why it should. – Stephen Ostermiller Feb 2 '18 at 17:24
  • I based this on older functioning of Apache back in the day when I studied Apache closely. At the time, all .Htaccess rules were cached and processed from top to bottom untill the processes cleared. You can still test this by creating rules that should process the modified request. This is why people get into condition loops. As to the various modules, I do not remember one versus the other. However, it is possible that my described scenario applies to rewrite and not to redirect. That needs to be tested. Good point! Cheers!! – closetnoc Feb 2 '18 at 17:56
  • @StephenOstermiller I forgot to flag you on my last comment. Sorry. Either way, the advice is the same. Create one matching rule. What would be the purpose of having more than one? None that I can see. Cheers!! – closetnoc Feb 2 '18 at 18:33
  • I have been doing some reading. The .Htaccess file is no longer cached. It is discovered and read per request. As well, the file is still processed in order from top to bottom and if more than one .Htaccess file exists such as in a subdirectory of the root, the rules are processed in the order they are found. This means reading the hierarchy in alphabetical order from top to bottom. This means that a rule in the root can supercede one in a subdirectory. This is the same behavior as before, just thought I would mention it. May add to the answer later. Still reading... – closetnoc Feb 2 '18 at 19:34
  • I should add that from Apache v.1.something to v.2.2 I was reverse engineering various installs and studying the behaviour using tools on executables when executed. Much has changed since the release of the v.2. I have not kept up on the details since v.2.2. – closetnoc Feb 2 '18 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.