How is a URL changed by more than one rewrite rule in htaccess?

I would like to ask a question to understand how Apache processes rewrite the rules specified in the .htaccess file.

On my site, I used the classic organization of pages in categories, each category of which has more than one section:

http://www.mysite.com/category/section.html. 

However, there are no html pages, everything is processed by the code in pages.php. Using a simple rewrite rule, URLs like the ones above map to:

 http://www.mysite.com/pages.php?cat=category&page=section 

I decided to rename the section from section1 to section1-xxx . To serve requests for the old name (section1), I added a simple rule to map section1.html to section1-xxx.html .

The first rules I added to .htaccess were as follows:

R 1

 RewriteRule ^CAT1/section1.html$ CAT1/section1-xxx.html [NC] 

where CAT1 is the name of the category.

R 2

 RewriteRule ^CAT1/(.*).html$ pages.php?cat=CAT1&page=$1 [L,NC] 

My idea was to use R1 and then R2. However, when these rules apply, I end up with an inexplicable (for my brain) URL.

When requesting the next page

 http://www.mysite.com/CAT1/section1.html 

The URL is first converted to

 http://www.mysite.com/CAT1/section1-xxx.html/section1.html 

then in

 http://www.mysite.com/pages.php?cat=CAT1&page=section1-xxx.html/section1 

Out of curiosity, I added the L flag (flag) to rule R1:

 RewriteRule ^CAT1/section1.html$ CAT1/section1-xxx.html [L,NC] 

and it worked fine. Now http://www.mysite.com/CAT1/section1.html is submitted via:

 http://www.mysite.com/pages.php?cat=CAT1&page=section1-xxx 

Now the questions are:

  • Why did I get this URL before adding the L flag to the R1 rule?
  • The L flag should indicate the application of the matching rule and the termination of the use of other rules. However, if the L flag is set, both R1 and R2 apply. Why?

Thank you for your time.

Regards, A.

+4
source share
2 answers

The [L] flag may have unexpected results if we do not know how the module works. Please note that the following applies only when mod_rewrite is used in the .htaccess file. The [L] flag behaves exactly as expected when used in httpd.conf.

The L flag tells Apache to stop processing rewrite rules for this request. Now what often is not implemented is that now it makes a new request for a new, rewritten file name and starts processing the rewriting rules again.

Therefore, if you have to do a rewrite where the destination still matches the pattern, it will not behave as it wishes. In these cases, you should use RewriteCond to exclude a specific file from the rule.

As you explained, add L to R1. (R1 redirects something.html to someone.html, i.e. you did not specify an html page!)

+3
source

1) you need the start / in the substitution line in R1

2), as amolv said, you have two iterations through the rewrite mechanism, in the first case both R1 and R2 corresponded, and the rewritten url did not comply with the rules in the second iteration; adding L to R1, the incoming url matched only R1 in the first iteration and only R2 in the second. When running / in R1 and R2, you no longer need L in R1.

I would like to know the contents of this section "XXX";)

+1
source

All Articles