SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    simple modrewrite, for a dummy ;)

    Howdy

    Can't test this right now, just need to know if it's correct..

    I need /articles/ to go to /articles.php, and /articles/article-title.html to go to /articles.php?article=$1

    Code:
    RewriteEngine On
    
    RewriteRule ^/articles/([A-Za-z0-9\-])\.html$ articles.php?article=$1 [L]
    RewriteRule ^/articles/?$ articles.php [L]
    Thanks
    Mike
    It's not who I am underneath, but what I do that defines me.

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Almost,

    this part will not work:
    RewriteRule ^/articles/?$ articles.php [L]
    the question mark quantifier doesn't make sense there, if it was to match the question mark in a query string, mod_rewrite doesn't match the query part of the string, so you should remove the question mark either way. You also don't need the first forward slash.

    Also this line:
    RewriteRule ^/articles/([A-Za-z0-9\-])\.html$ articles.php?article=$1 [L]
    You will probably need to change to:
    RewriteRule ^articles/([A-Za-z0-9\-]+)\.html$ articles.php?article=$1 [L]
    I highlighted the change. Without the plus, it will only attempt to match one character. Again no need for the first forward slash.

  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)
    Mike,

    You were fine - although there is NO need to escape characters within the square brackets. nullbit picked-up the single character of the first atom for you.

    nullbit,

    The question mark is the zero or one of the preceeding character and is part of regex. Mike's use was to make the directory's trailing slash optional and was correct as used.

    As for the use of the + to ensure that more than one character would be acceptable, I nearly missed that! Good pickup!

    Ditto the leading slash.

    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

  4. #4
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Cool, thanks for your help guys
    Mike
    It's not who I am underneath, but what I do that defines me.


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
  •