2

I need to rewrite all URLs in a specific format to their matching querystring'd URL and everything else should go to index.php?valid=false

I have:

RewriteRule (^[^\s]{4})/([0-9]{4})/(winter|fall|summer)/([0-9]{4})/?$ index.php?valid=true&subject=$1&course=$2&semester=$3&year=$4 [NC,L]
RewriteRule (.*)/?$ index.php?valid=false [NC,L]

The first rule is working fine: 4 letters, 4 numbers, fall or winter or summer, 4 numbers. And it seems like the second rule is working because everything is currently redirecting to the ?valid=false page. However, even URLs that are accepted by the first rule (when the 2nd one is commented out) pass through to the second one, even though they should be stopped by the [L] flag?

Am I doing it wrong?

Thanks!

tsdexter
  • 2,911
  • 4
  • 36
  • 59
  • 1
    perhaps see http://stackoverflow.com/questions/6797998/rewriterule-last-l-flag-not-working – user428517 Aug 02 '13 at 14:58
  • @sgroves thanks - that's an informative link - the answer below is working for me, but the rewritecond (or the helpful flowcharts) in that link might solve another issue I am having with a different rewrite. – tsdexter Aug 02 '13 at 16:28

1 Answers1

0

Replace your code with this:

RewriteRule (^[^\s]{4})/([0-9]{4})/(winter|fall|summer)/([0-9]{4})/?$ index.php?valid=true&subject=$1&course=$2&semester=$3&year=$4 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ index.php?valid=false [NC,L]

i.e. execute 2nd rule only when URI is not already index.php

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Awesome, it works. However, it introduces another issue I didn't think of. It is also rewriting my file include links such as CSS/Images/JS so they aren't returning the actual files needed to display the page properly. I seemed to have fixed that by including the following RewriteCond's before the 2nd rule - is this the correct way to do it without introducing other issues down the line? `RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d` – tsdexter Aug 02 '13 at 16:27
  • Yes indeed those 2 checks are needed to avoid rewriting for a file or directory. I have edited it accordingly. – anubhava Aug 02 '13 at 16:41