Forum Moderators: phranque

Message Too Old, No Replies

multiple url rewritings

multiple url rewritings

         

fahad direct

2:32 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



Hi,
Which one is more better way to keep server's load low if main folder say A has many subfolders say A-1, A-2, A-3 etc. and we have to do url mod rewrite:

1) Is it better to create one htaccess file inside folder A and put all mod rewrite patterns for the sub folders A-1, A-2, A-3 in that file?

OR

2) Should create independently htaccess file for each subfolder?

jdMorgan

4:37 pm on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You must put all external redirect rules in the top-level .htaccess file. You can put internal rewrite rules in any .htaccess file location.

This is necessary to prevent an internally-rewritten filepath from being exposed to the client as a URL by a subsequently-executed external redirect, which can result in 'ugly' and 'non-SEO-friendly' URLs being listed in search results for your site, and ruin your search engine rankings.

Some people prefer to put all rules in one top-level .htaccess file for ease of administration. Others prefer to put directory-path-specific rewriterules into .htaccess files in those directory-paths in order to achieve better efficiency. Some people use a mixed approach.

Really, only you can decide which approach is best for your site, taking into consideration the ease-of-administration/efficiency trade-offs.

However, either way it is important to bear in mind that execution order is important, and that no redirect should be allowed to execute after any internal rewrite has taken place. Further, it is best to avoid the possibility that more than one internal rewrite will be applied to any given HTTP request, as this can trigger a nasty Apache mod_rewrite bug which results in extra "copies" of the URL-path being re-injected into the server variable that holds the "current URL/filepath."

General guidance and best-practice: Across all configuration files and .htaccess files, put access controls first, followed by all external redirects, in order from most-specific to least-specific patterns and conditions, followed by all internal redirects, again in order from most- to least-specific.

"Most-specific" patterns and conditions will match only a single requested URL-path or a very few. "Least-specific" patterns and conditions will match many (or even all) requested URL-paths -- an example being a redirect from a non-canonical requested hostname to the canonical hostname, which should almost always be the last external redirect rule, and come before any/all internal rewrite rules.

I recommended putting the access controls first so as not to waste CPU time redirecting or rewriting requests from IP addresses or user-agents which are not welcome on your site. Just 403 them and be done with it.

Jim

fahad direct

1:27 am on Apr 30, 2010 (gmt 0)

10+ Year Member



Perfect thanks for all the details.