SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RewriteRule not matching regex correctly?

    I have the following RewriteRule in my .htaccess file.

    RewriteRule ^news/[a-z]+/([-a-z0-9]+)/$ http://localhost/story/$1/ [R=301]

    Its purpose is to send requests for

    http://localhost/news/category/story-title/

    to http://localhost/story/story-title/

    And it works fine for that purpose, but the problem is that if I have a link to http://localhost/news/category/ that link is now broken, because it's also trying to do a redirect on that.

    I've tested the above regex in several testing tools, and according to all of them, it should not be seeing news/category/ as a match, and yet on the server that's exactly what it seems to be doing. What can I do to fix this?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,077
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Yeah that really shouldn't match. Are you sure it isn't some other rule being fired? Can you post the complete .htaccess please?
    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
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Options +FollowSymLinks
    <FilesMatch "\.(html|htm|js|css)$">
    FileETag None
    <IfModule mod_headers.c>
    Header unset ETag
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
    </IfModule>
    </FilesMatch>


    RewriteEngine On


    RewriteRule ^news/[a-z]+/([-a-z0-9]+)/$ http://localhost/story/$1/ [R=301]

    RewriteRule ^story/([-a-z0-9]+)/$ /news.php [L]


    ErrorDocument 404 /404.html
    ErrorDocument 500 /404.html
    ErrorDocument 400 /404.html
    ErrorDocument 401 /404.html
    ErrorDocument 403 /404.html



    <Files 403.shtml>
    order allow,deny
    allow from all
    </Files>

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,077
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    And the category itself doesn't contain a slash? Like "food/drink" ?
    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

  5. #5
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    And the category itself doesn't contain a slash? Like "food/drink" ?
    Correct, they contain only letters a-z and hyphens.

    I've found that this actually works:


    RewriteRule ^news/[a-z]+/([-a-z0-9]+)/$ http://localhost/story/$1/ [R=301]
    RewriteRule ^story/[-a-z0-9]+/$ /news.php [L]
    RewriteRule ^news/.*/.*$ /news.php [L]

    but I hate having to use .* like that

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,077
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Oh, wait, that file is called news.php ...

    It may be that you have that stupid idiotic MultiViews option enabled (as you can tell, I'm not a fan..)

    Try adding Options -MultiViews at the top of your .htaccess
    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

  7. #7
    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)
    Rémon,

    No comment on the <IfModule> block (and it's repeated tests for every @#$%^ request?

    rst01,

    Wait a minute! You're redirecting from news/.*/.*/ to story/.*/ to news.php AND from news/.*/.* to news.php? Why the two step process you begin with? Why the "catch all" afterward?

    Also, if your
    Quote Originally Posted by rst01 View Post
    Correct, they contain only letters a-z and hyphens.
    Sorry, that's not what "[a-z]+" says - you're missing a hyphen at the start of the range definition (which you do have correctly for the story-title).

    Back to your original question, though: I believe that Rémon has hit on the likely culprit.

    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

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,077
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by dklynn View Post
    Rémon,

    No comment on the <IfModule> block (and it's repeated tests for every @#$%^ request?
    I was going to get to that when everything worked as it should
    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

  9. #9
    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)
    Rémon,

    Yeah, sorry, a pet peeve of mine (as you're well aware).

    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
  •