Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule Ignored but the first

rewriterule, htaccess,, mod_rewrite, urls

         

JayDev

12:32 am on Dec 19, 2004 (gmt 0)

10+ Year Member



I have the following issue with my .htaccess file
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ /mypage\.php?addition2=$1 [NC,L]
RewriteRule ^(.*)\.html$ /my-other-page\.php?addition3=$1 [NC,L]
RewriteRule ^index\.html$ /index\.php [NC,L]

It reads the first rewrite Rule but not the following ones. Any hint?

I also tried the same htaccess with [L] instead of [NC,L]

No success

Thanks for the help if you can.

sublime1

12:54 am on Dec 19, 2004 (gmt 0)

10+ Year Member



RewriteRules are processed in order. The [L] flag says "if this pattern matches, then do the rewite and stop processing any more".

Your first pattern

^(.*)\.html$
is the same as the second, and is more general than the third. So any page that ends in .html will get rewritten by the first rule, and no further rewriting will be done.

You need to define a regular expression that distinguishes between what's different between the incoming URL that would make one go to /mypage\.php?addition2=$1 and the other go to /my-other-page\.php?addition3=$1. If you're going to use the [L] flag (and it's probably a good idea), you need to put the specific cases first and the general cases later.

To see all of this in action, add RewriteLog and RewriteLogLevel directives so you can see what matches and why. Regular expressions are maddening :-)

JayDev

7:42 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



Could you give an example of "defining a regular expression".

If I change my htaccess to:

Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ /home-furnishing\.php?addition2=$1 [L]
RewriteRule ^home-furnishing/(.*)\.html$ /home-furnishing-article\.php?addition3=$1 [L]

Would this be making a difference? I am not sure I fully understand what you say i should. An example would help for someone slow like me :).

Thanks

sublime1

8:47 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



Rewrite rules use a pattern to match the incoming URL and do something with it. The pattern is defined as what is known as a "regular expression", one of the most dastardly, insidious, arcane, but powerful things ever invented in the computer world. Sometimes they are abbreviated as "regex".

Your first rewrite rule has one:
RewriteRule ^(.*)\.html$ /mypage\.php?addition2=$1 [NC,L]

First part: regular expression: ^(.*)\.html$
The ^ means "match the beginning of the request"
The ( and ) enclose, or "capture" a part of the URL that you can refer to later, in this case as $1
The . means "any character"
The * means "zero or more of the last character"
The \ means "treat the next character litterally", that is, look for a dot, don't interpret it as "any character"
The ".html" means it has to have ".html" in it
The $ means "and nothing else follows"

So all together, this pattern matches all URLs that end in .html, and saves everything before .html in the variable $1.

Second part: the URL to be created: /mypage\.php?addition2=$1
If the incoming page was /hello.html

the new url will be rewritten to /mypage.php?addition2=hello

Third part: flags: [NC,L]
NC = do the regex matching regardless of upper or lower-case, so hello.HTML, hello.html and hello.HtMl will all match
L = this is the last rewrite rule to process; don't look at any more.

Simple, isn't it :-)

Rewrite rules are processed in order until there are no more matches or until one has the [L] flag. So your second and third rewrites are never getting processed because the first regex matches all urls ending in .html (including index.html). Just moving the rewrite for index.html in front of the others should make it work. If you need to handle different kinds of URLs differently, you need regex's that identify what's different.

So you need to figure out what regular expression or expressions you need to match the pages you want to rewrite. Post a couple examples if you need help.