Page 1 of 1
Mod Rewrite... ARGH :(
Posted: Sat Sep 12, 2009 3:28 pm
by AlanG

They really do my head in lol ok here it is
I want to specify 2 rules, they work when separate, but not together. Anyone have any idea how to set up some sort of if condition?
Code: Select all
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change index.php?page=home to home.php
RewriteRule ^(.*)\.php$ index.php?page=$1 [L]
# Change event.php?event=party to event/party.php
RewriteRule ^event/(.*)\.php$ event.php?event=$1 [L]
Any help would be greatly appeciated. Thanks.

Re: Mod Rewrite... ARGH :(
Posted: Sat Sep 12, 2009 3:35 pm
by Darhazer
Your first rule captures all .php files, so the second one is never executed
Move the second rule before the first one.
Re: Mod Rewrite... ARGH :(
Posted: Sat Sep 12, 2009 3:48 pm
by AlanG
Darhazer wrote:Your first rule captures all .php files, so the second one is never executed
Move the second rule before the first one.
Thanks for the input. I tried that too but then neither rule works. :S
As it stands, the second rule isn't being executed. If I comment out one or the other, the opposite one works.
Re: Mod Rewrite... ARGH :(
Posted: Sat Sep 12, 2009 4:33 pm
by Darhazer
I think the rewrite conditions are applied only to the rule following them, this may be the reason the second one does not work
Re: Mod Rewrite... ARGH :(
Posted: Sat Sep 12, 2009 4:38 pm
by AlanG
Darhazer wrote:I think the rewrite conditions are applied only to the rule following them, this may be the reason the second one does not work
Hmm ok. Got any suggestions. I tried duplicating the conditions just above the second rule just to see what would happen and no joy.

Re: Mod Rewrite... ARGH :(
Posted: Sun Sep 13, 2009 6:10 am
by Darhazer
OK, I've tried it on my server and it loads only index.php too
No idea why this happening, I'll try to find out today
Re: Mod Rewrite... ARGH :(
Posted: Sun Sep 13, 2009 8:14 am
by AlanG
Darhazer wrote:OK, I've tried it on my server and it loads only index.php too
No idea why this happening, I'll try to find out today
Cool thanks. I've been at it for the last couple of hours today. Can't figure it out. I've sifted through the apache docs looking for appropriate flags. I don't consider myself an expert on apache so i'm kinda enjoying the learning. Is there a way to use the next rule if the current rule doesn't apply or anything?

Re: Mod Rewrite... ARGH :(
Posted: Sun Sep 13, 2009 10:19 am
by VladSun
Code: Select all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^event/(.*)\.php$ /index.php?event=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /index.php?page=$1 [L]
I've tried Darhazer's suggestions and this works for me (c).
Re: Mod Rewrite... ARGH :(
Posted: Sun Sep 13, 2009 11:04 am
by Eran
I've tried Darhazer's suggestions and this works for me (c).
Are you copyrighting your responses from now on?

Re: Mod Rewrite... ARGH :(
Posted: Sun Sep 13, 2009 11:08 am
by VladSun
pytrin wrote:I've tried Darhazer's suggestions and this works for me (c).
Are you copyrighting your responses from now on?

Well, indeed it should be :
I've tried Darhazer's suggestions and this
works for me (c).
I use this to emphasize that it works for me but there is no guarantee that it will work in general

Re: Mod Rewrite... ARGH :(
Posted: Sun Sep 13, 2009 11:30 am
by ridgerunner
VladSun wrote:Code: Select all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^event/(.*)\.php$ /index.php?event=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /index.php?page=$1 [L]
I've tried Darhazer's suggestions and this works for me (c).
Actually, you mistyped the first rule. It should rewrite to event.php not index.php. Otherwise, yes, this is the solution. Here is the corrected .htaccess:
Code: Select all
Options -Indexes
RewriteEngine on
# Change event.php?event=party to event/party.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^event/(.*)\.php$ event.php?event=$1 [L]
# Change index.php?page=home to home.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ index.php?page=$1 [L]
But to explain why the other variations did not work, it is helpful to look at the following simplified version (which does NOT work):
Code: Select all
RewriteRule ^event/(.*)\.php$ event.php?event=$1 [L]
RewriteRule ^(.*)\.php$ index.php?page=$1 [L]
At first glance this would appear to work. However, as it turns out, the "last" [L] flag does not really mean the
last. It means the last for this pass through the .htaccess file, but what is happening is that Apache is recursively calling the .htaccess file! (Read the Apache documentation for the LAST flag) I've experimented with this a bit and have figured out a bit of what is happening.
As an example, lets say you pass in a URL = "event/myevent.php". The first pass through .htaccess this matches the first rule and the URL is rewritten to be "event.php?event=myevent". This is what we want. But for some reason (I haven't figured it out yet), Apache passes this back through the .htaccess file a second time and this time, it matches the second rule and is rewritten to be "index.php?page=event". (Note that the query string from the first pass ("?event=myevent") is discarded during this second pass through .htaccess.) But Apache is not done yet! It runs .htaccess a third time, matches the second rule again and rewrites the URL to "index.php?page=index". At this point, further passes through .htaccess make no further changes. Its almost as if Apache runs the URL through .htaccess until the URL no longer changes.
The problem, I think is that the second rule creates an infinite loop - it takes in any .php file and rewrites it to be a .php file. Thus, there is an essential need for the RewriteCond statements.
Interesting problem!
Re: Mod Rewrite... ARGH :(
Posted: Mon Sep 14, 2009 11:32 am
by AlanG
:S Wow, lots to read there.

Thanks a bunch for the solution guys.
