Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ppc_michael

Guest
Original poster
Apr 26, 2005
1,498
2
Los Angeles, CA
I'm using mod_rewrite in my .htaccess file to rewrite URLs from this:
http://www.mysite.com/page/section/

To this:
http://www.mysite.com/index.php?page=$1&section=$2

Using this:
RewriteRule ^([A-Za-z0-9-]+)/?(?:([A-Za-z0-9-]+)/?)*$ /index.php?page=$1&section=$2 [NC,L]

This mostly works, but I'm seeing strange behavior when the "page" part of the URL is the same as an existing directory of the same name. So for example, if I want a page to load like this:

http://www.mysite.com/video/ to http://www.mysite.com/index.php?page=video&section=

But also a real directory exists at /video/, then what happens is instead of keeping the "clean" url like I want, it redirects to this:

http://www.mysite.com/video/?page=video&section=

Is there any way to fix this, besides renaming the "real" directory to something else? I have tried turning off indexes, but that did not work.
 
Not sure if it will fix it, but have you considered just rewriting it to something like index.php?url=$1

And use php to parse the url. This will almost certainly be faster that using regex on every incoming request. And give you more flexibility to have a wider variety of urls without rewriting .htaccess every time.
 
This mostly works, but I'm seeing strange behavior when the "page" part of the URL is the same as an existing directory of the same name.

did you add this before it?

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

to prevent it from applying if file or directory
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.