views:

47

answers:

2

I've got a site with the following .htaccess rule:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1
</IfModule>

It works great but I need to expand it so that IF there is another path taken by the user, I can forward it (but the root path should still work). I tried this, but the site just keeps processing the first RewriteRule:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1
RewriteRule /^(.*)$/^(.*)$ /$1.php?id=$2
</IfModule>

Any ideas?

So the root page could be

  • domain.com/doug so this is /index.php?id=doug
  • domain.com/dave so this is /index.php?id=dave

The inner path could be

  • domain.com/group/object1 so this is /group.php?id=object1
  • domain.com/group/object2 so this is /group.php?id=object2
  • domain.com/admin/login so this is /admin.php?id=login
A: 

can you provide an example of the URL in BOTH scenarios?

I'm not following what you're referring to as "another path"

Chris Bake
Updated, Chris!
Douglas Karr
A: 

Ok, I think you have to go about it differently.

The easy way would be to just pass everything to index.php, chop up the $_GET['id'], and switch($id[0]) on the root folder ('admin', 'group', etc..) as a parameter in your script. Perhaps even include("group.php") or admin.php inside the index.

Otherwise you're going to run into the problem of the root url's going to non-intended pages like: doug.php and dave.php

It can be done the current way you're headed, but you'll need to hard code cases for each root folder:

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/admin/(.)$ /admin.php?id=$1 [L,NC] RewriteRule ^/group/(.)$ /group.php?id=$1 [L,NC] RewriteRule ^(.*)$ /index.php?id=$1

You'll need these above the working RewriteRule line. That line should always be last, since it's the catch-all / nothing-else-matched / default case.

If hard coding the root pages is not an option, (too dam many or always unknown), you'd be better off in the long run to have your index.php just handle everything anyway.

Hope this helps, Thanks, Chris

Chris Bake
Thanks stackoverflow for making my code sample look like craphere's a better copy: http://pastebin.com/GeVTeBt1
Chris Bake