Jump to content

Cre8asiteforums Internet Marketing
and Conversion Web Design


Photo

.htaccess To Redirect Urls Without A Slash


  • Please log in to reply
12 replies to this topic

#1 A.N.Onym

A.N.Onym

    Honored One Who Served Moderator Alumni

  • Invited Users For Labs
  • 4003 posts

Posted 12 March 2007 - 12:47 AM

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, 12 March 2007 - 01:49 AM.


#2 bragadocchio

bragadocchio

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 15634 posts

Posted 12 March 2007 - 01:02 AM

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.

#3 A.N.Onym

A.N.Onym

    Honored One Who Served Moderator Alumni

  • Invited Users For Labs
  • 4003 posts

Posted 12 March 2007 - 01:17 AM

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, 12 March 2007 - 01:50 AM.


#4 bragadocchio

bragadocchio

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 15634 posts

Posted 12 March 2007 - 01:31 AM

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.

#5 A.N.Onym

A.N.Onym

    Honored One Who Served Moderator Alumni

  • Invited Users For Labs
  • 4003 posts

Posted 12 March 2007 - 01:55 AM

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.

#6 A.N.Onym

A.N.Onym

    Honored One Who Served Moderator Alumni

  • Invited Users For Labs
  • 4003 posts

Posted 12 March 2007 - 02:12 AM

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.

#7 JohnMu

JohnMu

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 3518 posts

Posted 12 March 2007 - 02:24 AM

Hi A.N.Onym

I don't know if you have seen this, but it has a lot of neat ideas on how to clean up URLs with mod_rewrite:

http://www.webmaster...che/3208525.htm
(now with even less cloaking ™ :naughty: )

John

#8 bragadocchio

bragadocchio

    Honored One Who Served Moderator Alumni

  • Hall Of Fame
  • 15634 posts

Posted 12 March 2007 - 02:36 AM

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.

#9 A.N.Onym

A.N.Onym

    Honored One Who Served Moderator Alumni

  • Invited Users For Labs
  • 4003 posts

Posted 12 March 2007 - 02:39 AM

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, 12 March 2007 - 02:44 AM.


#10 A.N.Onym

A.N.Onym

    Honored One Who Served Moderator Alumni

  • Invited Users For Labs
  • 4003 posts

Posted 12 March 2007 - 06:05 AM

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]


#11 Ruud

Ruud

    Hall of Fame

  • Hall Of Fame
  • 4887 posts

Posted 12 March 2007 - 08:55 AM

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.

#12 Tom Anthony

Tom Anthony

    Gravity Master Member

  • Members
  • 162 posts

Posted 12 March 2007 - 06:10 PM

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 ::

#13 A.N.Onym

A.N.Onym

    Honored One Who Served Moderator Alumni

  • Invited Users For Labs
  • 4003 posts

Posted 12 March 2007 - 07:18 PM

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, 12 March 2007 - 07:42 PM.




RSS Feed

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users