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

    Join Date
    Feb 2014
    Posts
    20
    Rep Power
    0

    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:
    Code:
    http://example.com/param1/param2
    and this would then redirect to a subdirectory within the server root something of the form:
    Code:
    /subdir1/subdir2/index.php?r=param1/param2
    What I have tried so far:
    In the main .htaccess file I have tried lots of variations similar to this:
    Code:
    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..

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

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,972
    Rep Power
    9647
    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.
    Code:
    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