Help me with Rewrite Rule

Discussion in 'HTML & Website Design' started by max196, Sep 13, 2009.

  1. #1
    Hi friends,

    My all site urls are dynamic & written by using RwriteRule in .htaccess file. All the url are like this,

    mysitename/page1/
    mysitename/page2/ and so on

    Problem is that, I have few backlinks like,

    mysitename/page1
    mysitename/page2

    whenever i open my webmasters tools under Crawl errors, these links count as Not Found. How should i change my rewrite url to show mysitename/page1 page as mysitename/page1/ my current rewrite rule is as bellow.

    RewriteEngine On
    RewriteRule ^([^/]*)/$ /detail.php?requested=$1 [L]

    Thank You!
     
    max196, Sep 13, 2009 IP
  2. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Make the slash optional; right now your match is insisting on it.

    ^([^/]*)/?$ detail.php?requested=$1
    So match anything that's not a slash, and then maybe a slash (for the end)

    the /? is kept out of the matched part and made optional.
     
    Stomme poes, Sep 13, 2009 IP
  3. max196

    max196 Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanx for the reply but it didn't worked.
     
    max196, Sep 14, 2009 IP
  4. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I should have added another slash after your real url:
    
    RewriteRule ^([^/]*)/$ /detail.php?requested=$1/ [L]
    
    Code (markup):
    with the / after the $1. If that doesn't work, I can show you this one from askapache that I thought went really far and I dunno your setup... but if you can try this offline/test! or something and see if it works for you:

    
    RewriteRule ^/*(.+/)?([^.]*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
    Code (markup):
    But I noticed your current rewrite didn't start with ^/ but just ^... apparently it matters which version of Apache you have (1.3 or 2.x)?

    It also does a permanent redirect for that slash, so that robots should notice and update their cache that the url is always with a /

    The above also takes care of longer urls: example.com/something/lastfile/
    where in the rewrite above (.+/)? can match "something"

    source
    I've never used it myself, I've just had the optional trailing slash in my matches.
     
    Stomme poes, Sep 15, 2009 IP