mod_rewrite with 2 parameters

Discussion in 'Apache' started by elHelgo, Nov 2, 2008.

  1. #1
    I'm trying to rewrite my URL that has two parameters without any luck. When people go to: www.site.com/blog.php?id=421&title=title+of+blog, I want the URL to show: www.site.com/blogposts/421/title+of+blog

    Here's the code that I'm using:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^/blogposts/([0-9]+)/([^/\.]+)/?$ /blog.php?id=$1&title=$2
    Code (markup):
    Any help would be greatly appreciated.

    Thanks.
     
    elHelgo, Nov 2, 2008 IP
  2. Rashy

    Rashy Peon

    Messages:
    20
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I also an not a mod_rewrite guru but do check the following links n check which one works for you:
    http://www.webmaster-toolkit.com/mod_rewrite-rewriterule-generator.shtml
    http://webmastersresort.com/mod-rewrite-generator/
    http://www.thejackol.com/htaccess-cheatsheet/
    http://www.generateit.net/mod-rewrite/
    http://xem247.com/tool/htaccess.php
    http://www.webmaster-money.org/tools/htaccess_generator.php
    http://www.searchenginegenie.com/mod-rewrite-generator.php
    Code (markup):
    Document:
    http://www.workingwith.me.uk/articles/scripting/mod_rewrite
    Code (markup):
     
    Rashy, Nov 4, 2008 IP
  3. tanenqq

    tanenqq Well-Known Member

    Messages:
    124
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Thank for share
     
    tanenqq, Nov 4, 2008 IP
  4. Rashy

    Rashy Peon

    Messages:
    20
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You are welcome tanenqq. Also if you want to read more on mod_rewrite the best book about it that i have read so far is
    http://www.amazon.com/Definitive-Guide-Apache-mod_rewrite/dp/1590595610
    Code (markup):
    if anybody want to get better understanding of mod_rewrite i would refer this e-book.
     
    Rashy, Nov 4, 2008 IP
  5. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Those rules look fine but the leading slash won't work in a .htaccess file. It will only work in an Apache configuration file. If these rules are in a .htaccess file then you will need to remove the leading slash.

    What that rule does is translates an external visible URL (the one people see in their browsers) of www.site.com/blogposts/421/title+of+blog to an internal URL (the one that your PHP script sees) of www.site.com/blog.php?id=421&title=title+of+blog. If you are requesting www.site.com/blog.php?id=421&title=title+of+blog in your browser then this rule won't do anything.

    The other problem that could cause this to not work in a .htaccess file is not having AllowOverride All in your Apache config.
     
    Ladadadada, Nov 5, 2008 IP
  6. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #6
    Options +FollowSymLinks +Indexes
    RewriteEngine on
    RewriteBase /
    RewriteRule ^blogposts/([^.]+)/([^.]+)$ blog.php?id=$1&title=$2 [L]

    And the script has to do the title+of+blog part, with some search and replace.
     
    Nintendo, Nov 6, 2008 IP