Jump to content

Cre8asiteforums

Web Site Design, Usability, SEO & Marketing Discussion and Support

Sign in to follow this  
A.N.Onym

.htaccess To Redirect Urls Without A Slash

Recommended Posts

Hello there.

 

I have recently noticed that I have a couple of duplicate pages that don't end with URLs.

 

So, I'd rather redirect them to the single variant - the URL with a slash (and not sure if I can do that with the robots.txt file).

 

How do I do this with a .htaccess?

 

Thank you.

Edited by A.N.Onym

Share this post


Link to post
Share on other sites

I'm not completely sure about what you are asking from the way that you've described it.

 

Can you provide an example, using example.com to stand for your URL?

 

Right now it sounds like you have something like this:

 

http://www.example.com/dir

http://www.example.com/dir/

 

The first one should be redirecting to the second one, but it sounds like it isn't

 

You could do that with something like what is described on this page:

 

Apache 1.3 URL Rewriting Guide

(see the appropriate page for your version of Apache if that isn't it)

 

There's a section on that page that deals with the "Trailing Slash Problem"

 

Though, when you say that you could do something with robots.txt to block one of these, I'm not sure if what I am writing about is what you mean.

Share this post


Link to post
Share on other sites

Bill, you got it right. I guess it could be quite hard to block any URL not ending with a slash from the robots.txt file.

 

Am I right to assume that the following code will be enough to be placed in the root domain for a WP blog?

 

RewriteEngine  on
RewriteRule	^(.+[^/])$		   $1/  [R]

 

Thanks.

Edited by A.N.Onym

Share this post


Link to post
Share on other sites
I guess it could be quite hard to block any URL not ending with a slash from the robots.txt file.

 

Right, because you would also be blocking the version with the slash by doing so.

 

The URL rewriting guide for the three different versions of Apache all show that code to handle a trailing slash in that instance. Some hosts don't allow mod rewrite on the htaccess level, and want something like this done within the server configuration, so it depends upon whether you can do that or not.

 

When you write:

 

I have recently noticed that I have a couple of duplicate pages that don't end with URLs.

 

Does that mean that when you go to those pages, without the trailing slash in them, they don't just redirect to the version with the slash? Or do you mean that you see those URLs in search results on the search engine, and that they do redirect? I can't tell from what you've written.

 

Regardless, if you have any links to that page from within the site, that is the default page of that directory, you want to make sure that those links include the trailing slash.

Share this post


Link to post
Share on other sites

I meant I have seen two URLs with and without the slash indexed by Google - apparently, either I or someone else linked to them that way.

Share this post


Link to post
Share on other sites

Well, I have put the following code, but it still doesn't work.

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME}  -d
RewriteRule ^(.+[^/])$ $1/ [R]

 

Could it be that the following WP code goes in the way?

 

RewriteCond %{REQUEST_FILENAME} !-d

 

Thanks again.

Share this post


Link to post
Share on other sites
I meant I have seen two URLs with and without the slash indexed by Google - apparently, either I or someone else linked to them that way.

 

Right, but does it automatically redirect or not, when there isn't a trailing slash?

 

Google may list the URL without the trailing slash if if sees it in a link somewhere.

Share this post


Link to post
Share on other sites

The URL without the slash doesn't redirect to the 'with the slash' version - and that's what I want the server to do.

 

Maybe it is a good time to learn .htaccess, after all.

 

Edit:

 

I have tried the code from the WMW thread above and a non-slash URL still isn't redirected to the with slash URL:

RewriteCond $1 !(/$¦\.html$¦\.)

Edited by A.N.Onym

Share this post


Link to post
Share on other sites

I have posted the question to my hosting support (with code sample) and they said I have full freedom in defining .htaccess.

 

Anyway, it appears the code below works only for the root page (the homepage), not for all pages - which I'd like to. What to I add to the code to apply it to all the pages? Thanks.

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME}  -d
RewriteRule ^(.+[^/])$ $1/ [R]

Share this post


Link to post
Share on other sites

From your mention of WP I understand you need this applied to WordPress?

 

Make life simple and get the permalink redirect. It solves your problem WordPress-wide: there is one URL per document and one URL only. If not, it redirects it (301). The backslash/no backslash URLs were what it was initially developed for.

 

Works good.

Share this post


Link to post
Share on other sites

I'd never considered that Google treats the URL with and without the slash as 2 different URLs. :/

 

Good thread. :lol:

 

:: runs off to fix sites ::

Share this post


Link to post
Share on other sites

Thanks, Ruud. I temporarily forgot I only, currently, need to fix it on the WP blog. It'd be nice to have a solution to all of my future sites, however.

 

Thanks again.

 

P.S. The plugin works!

Edited by A.N.Onym

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

×