natronp
natronp

Reputation: 23

Eliminate duplicate directory name in url using .htaccess file

For some reason, I'm getting duplicate directory names in some urls within a subfolder on our website. This seems to affect only crawlers as the files within this directory work fine when navigated.

I'd like to simply remove the duplicate directory name and make mydomain.com/sub/sub redirect to mydomain.com/sub.

I've tried many versions but my .htaccess skills are lacking apparently. I currently have (not working of course):

RewriteRule ^mydomain.com/sub/sub/(.*) mydomain.com/sub/$1  [L,R=301]

Upvotes: 2

Views: 550

Answers (1)

MrWhite
MrWhite

Reputation: 46012

RewriteRule ^mydomain.com/sub/sub/(.*) mydomain.com/sub/$1  [L,R=301]

The RewriteRule pattern matches against the URL-path only - you appear to have included (part of) the domain name. Also, mydomain.com in the substitution string is going to be seen as a relative subdirectory.

Assuming you have a limited number of subdirectories where this occurs then to reduce /sub/sub/<something> to just /sub/<something> you would do something like this:

RewriteEngine On
RewriteRule ^sub/sub/(.*) /sub/$1  [R=301,L]

If you have other directives in you .htaccess file, then this needs to go near the top.

First test with 302 (temporary) redirects to avoid potential caching issues. Clear your browser cache before testing.

But to echo @arkascha's comment... the reason why crawlers are finding these URLs in the first place would seem to be a fault in your URL structure/internal links - so this is what ultimately needs to be fixed.

Upvotes: 1

Related Questions