1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Rep Power

    Problems with Rewrite Rules

    Hello all,

    I've been having quite a few problems recently with apache URL Rewrite Rules. I have done a fair bit of reading and have tried lots of different variations although I'm still not getting it right so I have decided to post a message here as a last resort.

    The rewrite rules I need are quite simple on the face of it - I would like to rewrite URLs of the form:
    and this would then redirect to a subdirectory within the server root something of the form:
    What I have tried so far:
    In the main .htaccess file I have tried lots of variations similar to this:
    RewriteRule ^/(.*)/(.*)$     /subdir1/subdir2/index.php?r=$1/$2    [L]
    But nothing seems to work. I eventually will have more parameters on the end, although i just want to get the basics going so far..

  2. #2
  3. Headless Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Washington, USA
    Rep Power
    1. The leading slash on the input URL sometimes isn't there. You could learn when and where it is, but using /? is just easier.
    2. A URL rewriting regex should be as specific as possible. Using .* will match any amount of any thing, including nothing, and you're intending it to match a non-empty "path" component.
    3. This rewriting should only happen if the requested file doesn't already exist. Because technically /subdir1/subdir2/index.php matches that regex.
    4. A leading slash on the replacement is risky because it could be successfully interpreted as meaning the root directory, rather than the root of your site. Highly unlikely, but still, better off without.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/?([^/]+)/([^/]+)$ subdir1/subdir1/index.php?r=$1/$2 [L]

    Comments on this post

    • DewlanceHosting agrees : very helpful. You are soul of this forum.

IMN logo majestic logo threadwatch logo seochat tools logo