Htaccess and 301 redirects

Hi

I have some URLs indexed by Google, which are now old and no longer go to the page indexed (were search pages).

I’d like these URLs redirected to my homepage.

The URLs are in the format of:


http://www.exemple.co.uk/catalogsearch/result/index/?color=4&dir=asc&enable_googlecheckout=1&length=17&limit=15&order=relevance&price=3%2C10&q=product+test&ype=11

What I’m looking for really, is for anything that has ‘catalogsearch’ in the URL to redirect to the homepage, i.e. http://www.example.co.uk

Is this possible using a htaccess file?

Thanks

I dont’t think that will work, since the URLs have query strings and RewriteRule’s wont work. I think I need a RewriteCond, but I’m not sure how I’d match any URLs with ‘catalogsearch in them and redirect to the root domain…’

Try this:
.htaccess


Options +FollowSymlinks -Indexes

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /
	
  RewriteRule ^oldpage$ http://example.com/ [R=301,L]
</IfModule>

If you need to base on query string then use:

RewriteCond %{QUERY_STRING} ^id=8875&cat=234&sec=346$
RewriteRule ^index\\.php$ http://example.com [R=301,L]

Is there not an easier way of just matching the word ‘catalogsearch’ in the URL (http://www.example.co.uk/catalogsearch/blah/blah?id=1234567) and redirecting? Some URLs might differ in terms of the query string parameters, so I’d just like to match anything with catalogsearch, which is always first in the URL params.

I do not understand where is catalogsearch in URL or in query?

If you have the addresses like http://www.example.co.uk/catalogsearch?id=1234567
then just use like I have written before, because query does not matter:

RewriteRule ^catalogsearch\\.php$ http://example.com/ [R=301,L]

or

RewriteRule ^catalogsearch.\\.php$ http://example.com/ [R=301,L]

for
http://www.example.co.uk/catalogsearchasd
http://www.example.co.uk/catalogsearchb2
http://www.example.co.uk/catalogsearchsdas

If you read my initial post, I mentioned the URL:

http://www.exemple.co.uk/catalogsearch/result/index/?color=4&dir=asc&enable_googlecheckout=1&length=17&limit=15&order=relevance&price=3%2C10&q=product+test&ype=11

I need to be able to detect the ‘catalogsearch’ string in the URL, if I find it, redirect to http://www.example.co.uk - unfortunately, your example did not do this.

Adam, if you only want to check if a URL starts with something, don’t use the $ at the end :wink:

sipher, try this


RewriteEngine On

RewriteRule ^catalogsearch http://www.example.com/ [L,R=301]

:slight_smile:

so:


RewriteRule ^catalogsearch/.*$ http://example.com/ [R=301,L]

Ok, but that just removes the

catalogsearch/result/index/

I still have the query string params…

RewriteRule ^catalogsearch/.*$ http://example.com/? [R=301,L]

Just add question mark at the end.

Brilliant!!! That’s what I was missing. Thank you!

Yes:

Redirect 301 catalogsearch/ http://www.example.co.uk

I believe that will also kill the query string but not sure about this factor.

Adam,

NEVER leave <IfModule> tests in place on a production server as they will KILL the server’s performance.

As the other responder pointed out, the query string is unaffected by a mod_rewrite directive (which does not alter the query string). Also, the query string can be removed by adding a ? after the redirection - but, when mod_alias’s Redirect is the proper Apache tool to use and it doesn’t forward the query string, why bother? I’m a real fan of the power of mod_rewrite but the most important thing to understand is when to use it and when not to use it. Here, it’s better to use the proper tool: mod_alias’s Redirect.

Regards,

DK