Mod rewrite giving script name as first variable

Oh, the strange things mod_rewrite does!

Mod rewrite giving script name as first variable

Postby poizn99 » Wed Aug 01, 2007 12:24 am

Hi all

I'm fairly new to mod rewrite, so maybe my problem is I just dont know enough, but I think i'v found a really funny error in mod re write, can someone please help? This is also my first post in this forum, so if i'v made any mistakes, please tell me. Sorry that the post is a bit long, but either i'm really stupid or its something pretty interesting, so please carry on reading ;)

I have created a .htaccess file which looks like the following

Code: Select all
Options +FollowSymlinks
RewriteEngine On
RewriteBase /

#New rules for new mod re-write
rewriterule ^(.*)_type1$ test/index.php?cat1=$1&type=type1 [NC]
rewriterule ^(.*)\/(.*)_type1$ test/index.php?cat1=$1&cat2=$2&type=type1 [NC]
rewriterule ^(.*)\/(.*)\/(.*)_type1$ test/index.php?cat1=$1&cat2=$2&cat3=$3&type=type1 [NC]


I have one script in a "test" directory (which is in htdocs) called index.php
So my file structure looks like blah blah/htdocs/test/index.php
(ps the .htaccess file is in the test folder)

All im trying to do at the moment is check what variables mod rewrite sends to the script, so I only have some var_dumps in the script.

This is what index.php looks like

Code: Select all
<?php
  echo "GET <BR>";
  var_dump($_GET);
  echo "<br>";
  echo "POST <BR>";
  var_dump($_POST);
  echo "<br>";
?>


So I wanted to test the first rule
So I entered in a URL
(ps my machine name is dev)

http://dev/test/screws_type1

And got the following results

GET
array(2) { ["cat1"]=> string(6) "screws" ["type"]=> string(5) "type1" }
POST
array(0) { }

AWESOME!

Now lets try test the second rule
so i'm going to enter the following URL

http://dev/test/screws/bolts_type1

And this is now the result

GET
array(4) { ["cat1"]=> string(4) "test" ["cat2"]=> string(9) "index.php" ["cat3"]=> string(5) "bolts" ["type"]=> string(5) "type1" }
POST
array(0) { }

Firstly problem, I dont understand how its calling the third rule (I know this because there are 4 variables passed to the page). I mean, the .htaccess is in the test folder, so it shouldn't have taken the test directory as the first argument, right? I mean the first rule doenst count the test directory.

Second problem, the second variable passed has a value of "index.php" (the script name). Little bit strange!

Can anyone help me?
Many many thanks in advance ;)
poizn99
 
Posts: 8
Joined: Tue Jul 31, 2007 11:57 pm

Postby poizn99 » Wed Aug 01, 2007 1:10 am

Hi again.

Well after some more fiddleing around and some more reading on the net, I changed my .htaccess files to this

Code: Select all
Options +FollowSymlinks
RewriteEngine On
RewriteBase /

#New rules for new mod re-write
rewriterule ^([a-zA-Z0-9_]+)_type1$ test/index.php?cat1=$1&type=type1 [NC]
rewriterule ^([a-zA-Z0-9_]+)\/([a-zA-Z0-9_]+)_type1$ test/index.php?cat1=$1&cat2=$2&type=type1 [NC]
rewriterule ^([a-zA-Z0-9_]+)\/([a-zA-Z0-9_]+)\/([a-zA-Z0-9_]+)_type1$ test/index.php?cat1=$1&cat2=$2&cat3=$3&type=type1 [NC]


{I replaced the (.*) with ([a-zA-Z0-9_]+)}

And now it works
Go figure ;)

Hope this helps someone in the future
Bye
poizn99
 
Posts: 8
Joined: Tue Jul 31, 2007 11:57 pm

Postby richardk » Sat Aug 04, 2007 12:07 pm

You don't need a-z and A-Z if you have the NC flag and you should use L (to stop mod_rewrite if a rule matches).
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([a-z0-9_]+)_type1$                           test/index.php?cat1=$1&type=type1                 [NC,L]
RewriteRule ^([a-z0-9_]+)/([a-z0-9_]+)_type1$              test/index.php?cat1=$1&cat2=$2&type=type1         [NC,L]
RewriteRule ^([a-z0-9_]+)/([a-z0-9_]+)/([a-z0-9_]+)_type1$ test/index.php?cat1=$1&cat2=$2&cat3=$3&type=type1 [NC,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Idiosyncrasies

Who is online

Users browsing this forum: No registered users and 2 guests

cron