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.
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?
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, 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.
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
Dear Namjies, thank you for your help. I have the answer from webmasterworld and will try the solution to use mod_dir tommorrow.