301: site.com >> site.com/ (with end slash)

Discussion in 'Apache' started by Benedicte.new, May 19, 2010.

  1. #1
    Need only one redirect rule:

    From:
    To:
    (with end slash)

    Please, help if you can)
     
    Benedicte.new, May 19, 2010 IP
  2. earticles

    earticles Well-Known Member

    Messages:
    933
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    175
    #2
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*).htm
    RewriteCond %{REQUEST_URI} !(.*).html
    RewriteCond %{REQUEST_URI} !(.*).php
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://www.yoursite.com/$1/ [L,R=301]


    this redirects http://www.yoursite.com/directory to http://www.yoursite.com/directory/ and takes care not to add ending / for .htm, .html or .php pages

    for the homepage the ending / is added by default by the web server.
     
    earticles, May 19, 2010 IP
  3. Benedicte.new

    Benedicte.new Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    For the homepage without slash (http://site.com) the web server answers 200,
    but I need 301 redirect to http://site.com/.

    So I have 2 pages (http://site.com & http://site.com/), am I right?
     
    Benedicte.new, May 19, 2010 IP
  4. Namjies

    Namjies Peon

    Messages:
    315
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try this on index.php if you have php (before any text is sent to browser)
    <?php if ($url != "/"){header("HTTP/1.1 301 Moved Permanently"); header("Location: http://site.com/"); exit();} ?>
    Code (markup):
    and it should 301 redirect to Location: if the requested url is not a "/". if the requested file is http://site.com/index.php it would also change to Location:

    So no duplicates.

    Personally, it seems my server default to http://site.com/ if there's no slash, even if that's not in my .htaccess or php file. Or it might be Firefox who defaults to this.
     
    Namjies, May 19, 2010 IP
  5. Benedicte.new

    Benedicte.new Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Namjies, you see, all current browsers redirect homepage address to URL with end slash, so you can’t see real redirect from browser. If you want to view real situation you need to use redirect checker services, like http://www.internetofficer.com/seo-tool/redirect-check/ or http://www.webconfs.com/redirect-check.php. Why I need redirect? Because at first some search engines think that you have 2 duplicate pages, so they need time to paste together.

    I don’t know how to make php-redirect work in Wordpress, the only result which I obtained was cycle. I’ll try to talk with hoster.
     
    Benedicte.new, May 20, 2010 IP
  6. Namjies

    Namjies Peon

    Messages:
    315
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Oh, sorry!
    I had forgotten to set the value for $url by adding $url = $_SERVER['REQUEST_URI'];

    So $url was always null, so it was never "/" so it kept redirecting.

    Also, since it's wordpress, the header is used on all pages so we can't make the redirect happen when the url is not "/". That would redirect everything to homepage. Instead, we'll redirect when the url has a null value (those you don't want to be used).

    I tested it and it seems PHP consider "" to be the same as "/"

    I don't know if anything can be done. Try webmasterworld apache forums. There's 2 very responsive guys there which are real .htaccess experts. Maybe they could get something for you in .htaccess

    Personnally, couldn't find anything with a "blank" request uri working. My website is multi-language so no slash, slash or index.php , all of them redirects to /language/pagename
     
    Namjies, May 20, 2010 IP
  7. Benedicte.new

    Benedicte.new Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Dear Namjies, thank you for your help. I have the answer from webmasterworld and will try the solution to use mod_dir tommorrow.
     
    Benedicte.new, May 20, 2010 IP