Mod Rewrite... ARGH :(

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Mod Rewrite... ARGH :(

Post 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. :)
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Mod Rewrite... ARGH :(

Post by Darhazer »

Your first rule captures all .php files, so the second one is never executed
Move the second rule before the first one.
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Mod Rewrite... ARGH :(

Post 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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Mod Rewrite... ARGH :(

Post 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
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Mod Rewrite... ARGH :(

Post 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. :(
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Mod Rewrite... ARGH :(

Post 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
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Mod Rewrite... ARGH :(

Post 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? :dubious:
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Mod Rewrite... ARGH :(

Post 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).
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Mod Rewrite... ARGH :(

Post by Eran »

I've tried Darhazer's suggestions and this works for me (c).
Are you copyrighting your responses from now on? :lol:
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Mod Rewrite... ARGH :(

Post by VladSun »

pytrin wrote:
I've tried Darhazer's suggestions and this works for me (c).
Are you copyrighting your responses from now on? :lol:
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 :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Mod Rewrite... ARGH :(

Post 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!
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Mod Rewrite... ARGH :(

Post by AlanG »

:S Wow, lots to read there. :) Thanks a bunch for the solution guys. :)
Post Reply