Frantique
Frantique

Reputation: 175

Redirect non-existing subfolders with .htaccess

I have a site where I have some links like this: http://mysite.com/search-RANDOM_WORDS.htm
The problem is that Google tries to index links like:

http://mysite.com/search-RANDOM_WORDS.htm/existing-link-on-the-site
http://mysite.com/search-WORD.htm/link-on-the-site/search-ANOTHER_WORD.htm

etc. etc. The site doesn't use subfolders in links, every link is on the root. I don't really understand from where Google is taking these links through, but I have to fix it somehow. I am using mod_rewrite already, but I did not found yet a working solution for my problem. It should work like this:

  1. If the link what is trying to access contains a subfolder which doesn't exist on the server
  2. reditect it to the root with 404 error.

How can be done this? Thanks!

Upvotes: 1

Views: 374

Answers (1)

anubhava
anubhava

Reputation: 785481

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^(search-.+?\.htm)/.*$ /$1 [L,R=301,NC]

Upvotes: 1

Related Questions