mod_rewrite+.htaccess Behavior

Discussion in 'Web Design and Development' started by ppc_michael, Apr 14, 2012.

  1. ppc_michael Guest


    Apr 26, 2005
    Los Angeles, CA
    I'm using mod_rewrite in my .htaccess file to rewrite URLs from this:

    To this:$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: to

    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:

    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.
  2. jared_kipe macrumors 68030


    Dec 8, 2003
    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.
  3. clemsonhomerun macrumors newbie

    Jul 11, 2009
    did you add this before it?

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

    to prevent it from applying if file or directory

Share This Page