Mod Rewire (Rule)

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

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Mod Rewire (Rule)

Post by AliasBDI »

I have some working rewrite rules. The first group of lines tell the server to parse all urls with folder paths as query strings. For example, domain.com/string1/string2/ would be parsed as domain.com/details.php?var1=string1&var2=string2.

Code: Select all

RewriteEngine On
RewriteRule   ^([A-Za-z0-9-]+)/?$   /details.php?nav_ID=$1
RewriteRule   ^([A-Za-z0-9-]+)(/([A-Za-z0-9-]+))/?$   /details.php?nav_ID=$1&pg_ID=$2

# eControl rewrite
RewriteRule   ^/eControl(/([A-Za-z0-9-]+))/?$   /eControl/index.php
RewriteRule   ^/eControl$   /eControl/index.php
RewriteRule   ^/eControl/$   /eControl/index.php
My problem is that I have a particular folder in the web site called 'eControl' that should not follow that rule. So I added a rule below it that rewrites this. For example, domain.com/eControl/string would be parsed as domain.com/eControl/string. However, it is not working. What I need is the script to tell the server to ignore all url strings with "eControl" in them. Any ideas?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Mod Rewire (Rule)

Post by superdezign »

Maybe it's following the first rules first, so by the time the URL gets to the eControl rules, it's already been changed.

Can't rewrite rules have conditions BEFORE they actually occur? Like, if the folder name is not eControl, THEN mod_rewrite?
Last edited by superdezign on Tue May 29, 2007 9:06 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Place them first.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

I suggest checking out the apache mod_rewrite documentation, it's very helpful;
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Ahhhh ... I used put the lines at the top as suggested and then changed the directive from RewriteRule to RewriteCond. Thanks for the help. Works fine now.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Well, apparently that was my cache. It really does not work. Two lines have conflicts:

Code: Select all

RewriteCond   ^/eControl(/([A-Za-z0-9-]+))/?$   /eControl/index.php$todo=$1

RewriteRule   ^([A-Za-z0-9-]+)/?$   /details.php?nav_ID=$1
I tried a varity of ways with this code, but evidently, the order is correct, but they are conflicting. Any ideas?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Change it back to RewriteRule instead of RewriteCond and try again ;)
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

If it is set back to RewriteRule then it is ignored all together.

Code: Select all

RewriteRule   ^eControl/([A-Za-z0-9]+)/?$   /eControl/index.php$todo=$1

RewriteRule   ^([A-Za-z0-9-]+)/?$   /details.php?nav_ID=$1
RewriteRule   ^([A-Za-z0-9-]+)(/([A-Za-z0-9-]+))/?$   /details.php?nav_ID=$1&pg_ID=$2
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I'm not very experienced in mod_rewrite but, last I checked, RewriteCond applied to the RewriteRule that it is paired with. So, make your RewriteCond only let the RewriteRule apply when eControl is not in the URL (Unless, of course, I'm mistaken).

Code: Select all

RewriteCond %{REQUEST_URI} (^eControl)
I think...
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Didn't work ... I'll investigate that though. Thanks.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

AliasBDI wrote:Didn't work ... I'll investigate that though. Thanks.
Yeah, I'm sure my regex is wrong, but I'm positive that this is the way the RewriteCond works.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

I got it to work! This code worked:

Code: Select all

RewriteEngine On

RewriteCond %{REQUEST_URI} !(/eControl)
RewriteRule   ^([A-Za-z0-9-]+)/?$   /details.php?nav_ID=$1

RewriteCond %{REQUEST_URI} !(/eControl)
RewriteRule   ^([A-Za-z0-9-]+)(/([A-Za-z0-9-]+))/?$   /details.php?nav_ID=$1&pg_ID=$2
It will rewrite all URL strings except those that begin with /eControl.
Post Reply