1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Modrewrite and trailing slashes

Discussion in 'Apache' started by ozgression, Apr 17, 2006.

  1. #1
    Ok, so I have the following in my htaccess:

    Options +FollowSymLinksRewriteEngine onRewriteRule ^(.*)/?$ index.php?page=$1 [QSA,L]
    Code (markup):

    Now, that works great for site.com/page , but doesnt work for site.com/page/ <-- note the trailing slash.

    What I want to do is have a trailing slash and if you type the url without the trailing slash you are forced to the trailing slash version (make sense?).

    This sort of stuff makes no sense to me and I assume (hope) that this something simple that a good coder knows how to fix, simply. :)

    Any help is appreciated.
     
    ozgression, Apr 17, 2006 IP
    rickbender1940 likes this.
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule ^(.*[^/])$ $1/ [R=301]
    RewriteRule ^(.*)/$ index.php?page=$1 [QSA,L]
    Code (markup):
     
    exam, Apr 17, 2006 IP
  3. ozgression

    ozgression Well-Known Member

    Messages:
    343
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Hi exam,

    I tried that code, but it didnt work. Thanks anyway, though. :)
     
    ozgression, Apr 18, 2006 IP
  4. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Let's see, you might have to escape the slash
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule ^(.*[^\/])$ $1/ [R=301]
    RewriteRule ^(.*)\/$ index.php?page=$1 [QSA,L]
    Code (markup):
    What error did it give you?
     
    exam, Apr 18, 2006 IP
  5. ozgression

    ozgression Well-Known Member

    Messages:
    343
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    140
    #5
    Hi, still didn't work. I am getting this error:

    Moved Permanently
    The document has moved here.

    Additionally, a 400 Bad Request error was encountered while trying to use an ErrorDocument to handle the request.

    ... if I use trailing slashes or not.
     
    ozgression, Apr 18, 2006 IP
  6. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #6
    1. What browser are you using to view your site in?
    2. Do you have anything else in your .htaccess file? If so, can you post it here?
     
    exam, Apr 18, 2006 IP
  7. ozgression

    ozgression Well-Known Member

    Messages:
    343
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    140
    #7
    Hi... there is nothing else in my htaccess.

    Here is the index.php file, though. If you know of a better way of doing all this, by all means, feel free to let me know. :)

    <?php
    require('/home/mysite/config.php');
    	
    if($_GET['page']) {
    $page = $_GET['page'];
    $q = mysql_query("SELECT * FROM cms WHERE name = '$page'");
    if(mysql_num_rows($q)) {
    // insert the correct page
    $page_info = mysql_fetch_array($q);
    } else {
    // the requested page is not in the db, default to home
    $page_info = mysql_fetch_array(mysql_query("SELECT * FROM cms WHERE name = 'home'"));
    $page = 'home';
    }
    } else {
    // there's no query string, default to home
    $page_info = mysql_fetch_array(mysql_query("SELECT * FROM cms WHERE name = 'home'"));
    $page = 'home';
    }
    ?>
    <html>
    <head>
    <title><?=$page_info['title']?></title>
    <meta name="keywords" content="<?=$page_info['meta_kwds']?>" />
    <meta name="description" content="<?=$page_info['meta_desc']?>" />
    </head>
    <body>
    <?php
     include($page . '.inc');
    ?>
    </body>
    </html>
    
    Code (markup):
     
    ozgression, Apr 18, 2006 IP
  8. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #8
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule ^([^.]+)$ index.php?page=$1 [QSA,L]


    You can add

    RewriteRule ^([^.]+)/$ index.php?page=$1 [QSA,L]

    before the one RewriteRule if you need to.
     
    Nintendo, Apr 18, 2006 IP
    rickbender1940 likes this.
  9. ozgression

    ozgression Well-Known Member

    Messages:
    343
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    140
    #9
    I'll give that a try... thanks.
     
    ozgression, Apr 19, 2006 IP
  10. ozgression

    ozgression Well-Known Member

    Messages:
    343
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    140
    #10
    Hehehe... it works! Thanks.
     
    ozgression, Apr 20, 2006 IP
  11. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #11
    The Wacko mod-rewrite master has come thru again.
     
    exam, Apr 22, 2006 IP