Periods in RewriteRule give an Internal Server Error

Discussion in 'Apache' started by jonfleck, Feb 11, 2010.

  1. #1
    Here is my old rewrite rule.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([a-zA-Z0-9\-_]+)$ /index.php?name=$1 [L]
    </IfModule>

    The old rule works great but I wanted to add the ability for users to use have periods in the url. For example, in the following URLs periods seem to make the most sense:

    en.opensuse.org/OpenSUSE_11.2

    instead of en.opensuse.org/OpenSUSE_11_2

    or

    en.wikipedia.org/wiki/Mac_OS_X_v10.1

    instead of en.wikipedia.org/wiki/Mac_OS_X_v10_1

    As you can see, having periods in the URL makes for a more readable URL.

    I added a period to the rule and now I receive an Internal Server Error.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([a-zA-Z0-9\-_.]+)$ /index.php?name=$1 [L]
    </IfModule>

    If somebody could please let me know what I'm doing wrong and how to correct the problem it would be greatly appreciated.
     
    jonfleck, Feb 11, 2010 IP
  2. fierceservers

    fierceservers Peon

    Messages:
    338
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Using just a "." would result in a match for just about any expression. It needs to be escaped using a backslash:
    Give that a try, can't guarantee it'll work though :)
     
    fierceservers, Feb 11, 2010 IP
  3. jonfleck

    jonfleck Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I just tried the following

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([a-zA-Z0-9\-_\.]+)$ /index.php?name=$1 [L]
    </IfModule>

    Still no luck. Also, I don't think a period needs to be escaped when using brackets

    'Within POSIX bracket expressions, the dot character matches a literal dot. For example, a.c matches "abc", etc., but [a.c] matches only "a", ".", or "c".'

    http://en.wikipedia.org/wiki/Regular_expression#POSIX_Basic_Regular_Expressions

    Thanks for the suggestion though.
     
    jonfleck, Feb 12, 2010 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    By any chance did you try any of the dot URLs before implementing the new RewriteRule ?
    I can't think of any reason adding a dot to your pattern would cause an internal server error, I'm wondering if it's something to do with file extensions or script handlers since dot usually sets that stuff in motion and perhaps the timing of the RewriteRule modification could just be masking that.

    Though now that I think about it, your pattern does look pretty much all-encompassing now. It would match just about anything now wouldn't it ?
    Maybe it's a rewrite loop.

    --
    While we're on the subject.

     
    joebert, Feb 12, 2010 IP
  5. jonfleck

    jonfleck Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The rule is needed send my PHP the information needed to display the correct content. Then the PHP script displays the proper content depending on the URL. As far as I can tell the expression is correct according the REGEX rules. For some reason Apache doesn't like the period in the rule.
     
    jonfleck, Feb 14, 2010 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    Well, after adding the dot your pattern will match "index.php" as well as everything else someone is likely to request. Everything you request will trigger the pattern, even the result of the rewrite during the next round of rule application.

    You'll need to come up with something you can add to the result URL that can be checked for on the next round of rule application to exclude the result URL from the rewrite.
     
    joebert, Feb 15, 2010 IP