1

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

1 Answer 1

1

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:

Example:

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks stackoverflow for making my code sample look like crap here's a better copy: pastebin.com/GeVTeBt1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.