Add Trailing Slash /

Discussion in 'Apache' started by lostdog, Mar 9, 2011.

  1. #1
    Hi,

    I am tring a mod rewrite to make it so all urls that need a trailing slash get it. If entered (www.mydomian.com/alabama changes to www.mydomian.com/alabama/) The following code works but also adds the "/" to urls with the .html etc.. e.g.(www.mydomian.com/contactus.html is now www.mydomian.com/contactus.html/)

    What am I doing wrong?

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !example.php
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://www.mydomain.com/$1/ [L,R=301]
     
    Last edited: Mar 9, 2011
    lostdog, Mar 9, 2011 IP
  2. eleetgeek

    eleetgeek Peon

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Pretty tricky eh!

    Here is a workaround that you can do in your index.php file
    
    $uri = $_SERVER['REQUEST_URI'];
    $check1 = strrev($uri);
    $check2 = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."/";
    if ($check1(0) != "/")
    {
    header ("location: $check2"); // Do not use single quote here
    }
    
    
    PHP:
    But, this 'may' impact ur SEO, we are using header change request, so make sure, this is above ENTIRE code!


    PS: I haven't tried the code but, it just might work ;)
     
    eleetgeek, Mar 10, 2011 IP