mod_rewrite+.htaccess Behavior

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

  1. ppc_michael Guest

    ppc_michael

    Joined:
    Apr 26, 2005
    Location:
    Los Angeles, CA
    #1
    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.
     
  2. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #2
    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

    Joined:
    Jul 11, 2009
    #3
    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