SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with URL Rewrite rule

    I've been trying to figure this out, but can't seem to get it to work. If anyone could help out by letting me know if my rule is correct or not, I can maybe figure out what the issue might be if its somewhere else.

    Its a pretty standard rewrite, i.e. to change:

    www.mydomain.com/subfolder/index.php?LodgeID=123

    to

    www.mydomain/subfolder/123/

    I thought it should be something like:

    RewriteRule ^/lodge/([0-9]+)$ /index.php?LodgeID=$1

    Bu that doesn't seem to do anything - am I barking up the right tree?

    Thanks.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    I assume you do have

    Code:
    RewriteEngine On
    somewhere in your code?

    Other than that, you must remove the first slash, and add a slash at the end, like so

    Code:
    RewriteRule ^lodge/([0-9]+)/$ /index.php?LodgeID=$1 [L]
    If also added a [L] flag, to make sure that if the rule matches mod_rewrite will stop the current round of rewriting.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    JG,

    RewriteEngine on simply ensures that the mod_rewrite engine is not in the comment mode, i.e., required only in unusual circumstances. Rémon is spot-on removing the leading / (in the DocumentRoot with this mod_rewrite code) and Last flag but I take exception to changing the directory level TWO times with that trailing / of his (it will impact the directory level of your relative links).

    It's a very minor point that the leading / in the redirection makes Apache look first to the SERVER's root THEN to the domain's DocumentRoot (where you have this code - so you don't need that /). If an index.php exists in the server's root, it will receive the redirection. Of course, any other filename COULD result in a security issue.

    With those explanations, I would recommend:

    Code:
    RewriteEngine on
    RewriteRule ^subfolder/([0-9]+)$ index.php?LodgeID=$1 [L]
    Note that your "subfolder" need not exist (and probably shouldn't) as it's only being used as a "marker" to activate the mod_rewrite redirection.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •