SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot OrangeCreative's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RewriteRule question

    Hi,

    I'd like to use a RewriteRule that would convert http://www.mysite.com/the_work to http://www.mysite.com/index.php?page=the_work.

    The reason is I'd like to be able to use $_GET['page'] to identify the current page while offering a SEO-friendly URL to the page.

    What I planned to do is like this:

    Options +FollowSymLinks
    RewriteEngine on

    RewriteRule ^the_work/ /the_work [R]
    RewriteRule ^the_work /index.php?page=the_work

    Is this the right way of doing this?

    Many thanks in advance!

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    Macomb, MI USA
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    RewriteEngine On
    RewriteRule /([^/]*)$ /index.php?page=$1 [L]
    inClick Ad Server - Start Your Own Performance Based Ad Network
    Key Features | Download inClick Now | Free Ad Server Hosting Offer

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

    First, your code is deficient in that it requires that the {REQUEST_URI} match BOTH the_work/ and the_work successively (those are ANDed in your code) then the page will be redirected to /index.php and then be ANDed with the following mod_rewrite code.

    THe problems can easily be resolved by using the optional notation and using the Last flag to terminate the block statement (as you would with ; or }). Sometimes, too, Apache does NOT like to see the leading / in the redirection as it confuses the DocumentRoot with the server's physical root - but that's with an Apache that's ready to fall over.
    Code:
    RewriteEngine on
    RewriteRule ^the_work/? index.php?page=the_work [L]
    inclick,

    Thanks for the help but your code will generate an infinite loop on Apache 1.x and will not work at all on Apache 2.x.

    The Apache version problem is caused by the leading / in your regex which is required for a DocumentRoot in Apache 1.x and will prevent a match with Apache 2.x. If you're not sure which version you're using, either don't use the start anchor (^ - which you didn't use) or make it optional (/?).

    The "loopy" problem is that your redirection will be matched on the next pass through mod_rewrite, i.e., ([^/]*) will match index.php and loop.

    To me, using a "catch-all" like ([^/]*) is almost as bad as using the dreaded EVERYTHING atom ( the (.*) ) as it only eliminates the / from the list of matching characters. BE SPECIFIC in what you match to avoid problems (and check that the regex will not match the 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

  4. #4
    SitePoint Zealot OrangeCreative's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you guys! I knew I'll be able to find the answer here!

    I will be playing around with the suggested solution to see what sticks. Thanks again!

  5. #5
    SitePoint Zealot OrangeCreative's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works like a charm! Thanks DK! Thanks inclick for taking the time to answer!

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

    No problem! That's what I'm here for (helping you LEARN).

    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
  •