SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    New York
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod_rewite site.com/xx to content.php?id=xx

    Hi,

    I have a unique problem that I've not been able to find answers too because it involes passing a variable after the first slash... basically I want to have

    site.com/xx redirect to content.php?id=xx using mod_rewrite

    If I just do

    Code:
     Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^(.*)  /content.php?id=$1 [R,L]
    My site doesn't load at all. Does anyone have any ideas?
    TightBeatZ.com: Free Underground Hip-Hop loops
    Pregaming.com: Game Videos and Trailers

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

    Code:
    RewriteEngine On
    RewriteRule ^(.*)  /content.php?id=$1 [R,L]
    will cause a LOOP! What you've told it is to feed EVERYTHING to the content.php script as the query string. That means that content.php will become the query string (first time through) then it'll keep replacing itself in the query string until Apache collapses.

    You could use a RewriteCond(ition) to eliminate content.php from EVERYTHING
    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^content\.php$
    RewriteRule ^(.*)  /content.php?id=$1 [L]
    says that IF the requested page must NOT be content.php, then redirect EVERYTHING to content.php. If that's the only page on your site, fine!

    Rather than use EVERYTHING, don't you know what values to expect? List them, then, in the RewriteRule:
    Code:
    RewriteEngine On
    RewriteRule ^(aa|bb|xx)$  /content.php?id=$1 [L]
    will dutifully rewrite aa, bb or xx as the query string for content.php.

    Okay?

    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

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    New York
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for your help. I have implemented each of your suggestions and here are the results:

    Code:
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^content\.php$
    RewriteRule ^(.*)  /content.php?id=$1 [L]
    Creates a 500 Interal Server Error (note the [L] at the end). The RewriteRule line is what causes the error.

    Code:
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^content\.php$
    RewriteRule ^(.*)  /content.php?id=$1 [R,L]
    Creates a permanent loop (note the [R,L]) at the end

    Code:
    RewriteEngine On
    RewriteRule ^(aa|bb|xx)$  /content.php?id=$1 [L]
    Works perfectly - however I have about 3000 variables so I'm not sure if this is the most efficient solution.

    Thanks again for your help. I will keep playing around with it. Does anyone else have any suggestions?
    TightBeatZ.com: Free Underground Hip-Hop loops
    Pregaming.com: Game Videos and Trailers

  4. #4
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What allowed characters are allowed in your variables? Then we can help you write a slightly more efficient regexp to match it

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    New York
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    numbers (0-9), letters (a-z and A-Z) and underscores (_) are used.

    Thanks!
    TightBeatZ.com: Free Underground Hip-Hop loops
    Pregaming.com: Game Videos and Trailers

  6. #6
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then use this baby:

    Code:
    RewriteEngine On
    RewriteRule ^([A-Za-z0-9_])/?$  /content.php?id=$1 [L]
    Last edited by Dean C; Feb 22, 2005 at 05:35.

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

    Dean's done it again!

    As we've both suggested to you, it's better to ACCURATELY define your redirection list but, from the information you've provided, that's the best that can be offered.

    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
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your rep isn't worthy of your helpfulness David Keep up the good work, I'll be there waiting to post it before you hehe

  9. #9
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ack my code above is wrong. Be sure to use this instead:

    Code:
    RewriteEngine On
    RewriteRule ^([A-Za-z0-9_]+)/?$  /content.php?id=$1 [L]


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
  •