Question

There are bad links left over from a previous error, and I'm trying to clean up my mess with 301 RedirectMatch.

Correct URLs are the following format:

http://www.example.com/aaa-directory/page
http://www.example.com/bbb-directory/page
http://www.example.com/ccc-directory/page

(etc.)

But, previously there were bad links that linked to the pages at:

http://www.example.com/aaa-directory//page
http://www.example.com/bbb-directory//page 
http://www.example.com/ccc-directory//page

(double backslash // when should be single / )

I've fixed the bad links, but I still get external links and/or robot crawls that go to those bad URLs.

I'd like to use a RedirectMatch 301 so that any link with the text "-directory//" will redirect to the same link with "-directory/".

So, http://www.example.com/ccc-directory//page would be redirected to: http://www.example.com/ccc-directory/page

I'm struggling with the syntax. Any help would be appreciated!

(not sure if this is needed, but my current HTACCESS file is as follows):

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Was it helpful?

Solution

Keep redirect rules before internal rewrite ones and have another rule to take care of //

RewriteEngine on
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+(.*?)/+(/\S*) [NC]
RewriteRule ^ %1%2 [R=301,L,NE]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top