1

I have a little problem on Apache's rewrite rules

Here's my rules

Options +FollowSymLinks
RewriteEngine On

RewriteBase /

RewriteRule query/(.*) /php/query.php?name=$1 [L]
RewriteRule page/(.*) /php/page.php?page=$1 [L]

It works perfectly. But when I try to add the following rule to rewrite URL that not matches the two previous rules

RewriteRule .* /php/page.php?page=home

The server responds "Internal server error". Why ?

guy777
  • 222
  • 1
  • 14
  • Look into your server’s error log! (Most likely you have created an endless redirect here, because `.*` of course matches `php/page.php` as well.) – CBroe Nov 06 '13 at 10:20
  • The second rule match before .* – guy777 Nov 06 '13 at 10:27
  • 1
    No, not for the (internal) request for `/php/page.php?page=home` – CBroe Nov 06 '13 at 10:29
  • The rules are stored in .htaccess file. The documentation say in that case the rewrited request is passed again to the engine. – guy777 Nov 06 '13 at 10:37

2 Answers2

4

You can use RewriteLog to see what happends, but even with a [L] you can be sure a rewrited url is always re-checked on the set of rules (this is call internal redirect).

So add some RewriteCond before this final catch-all, or prevent it to be running on internal redirects, this way:

RewriteCond %{ENV:REDIRECT_STATUS} ^$

You could also add the [NS] or [nosubreq] tag on your final catch-all, which does the same.

But preventing your rule on internal redirection also means it will never be applied after any previous rule, and one day you might want it. So be careful with next rules.

regilero
  • 29,806
  • 6
  • 60
  • 99
0

Thanks !

Finally, here's my solution :

Options +FollowSymLinks
RewriteEngine On

RewriteBase /

RewriteRule query/(.*) /php/query.php?name=$1 [L]
RewriteRule page/(.*) /php/page.php?page=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ page/home [L,R]
RewriteRule ^$ page/home [NS,R]
guy777
  • 222
  • 1
  • 14