Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess redirect for parent but not children?

I have a number of URLs that I need to redirect to new locations, but there are some situations where child pages need to remain active and not redirected. For example:

/products would redirect to http://www.newsite.com/products

/products/category1 would redirect to http://www.newsite/products/category1

But /products/specialitem would not get redirected at all.

Is this possible with either Redirect or RedirectMatch?

Doing a Redirect 301 /products http://www.newsite.com/products seems to affect all child pages

Thanks for any guidance!

Edit:

Using waynethec's answer, I was able to get started. But can anyone clarify why my first rule below works but the others do not?

RedirectMatch 301 ^segment-one$ http://www.google.com/

RedirectMatch 301 ^segment-one/segment-two$ http://news.google.com/

RedirectMatch 301 ^segment-one/segment-two/segment-three$ http://cnn.com/

RedirectMatch 301 ^segment-one/segment-two/segment-three/foobar$ http://gbv.com/

(By not working, I mean that I still can get to the pages, rather than them getting redirected.)

like image 457
clorentzen Avatar asked Oct 12 '14 00:10

clorentzen


People also ask

What is 301 .htaccess redirect option?

A 301 signals a permanent redirect from one URL to another, meaning all users that request an old URL will be automatically sent to a new URL. A 301 redirect passes all ranking power from the old URL to the new URL, and is most commonly used when a page has been permanently moved or removed from a website.

How can I redirect and rewrite my URLs with an .htaccess file?

Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.

How do I remove htaccess redirect?

Simply remove the lines from your . htaccess . In case it does not work your browser may have cached the 301 redirect, as it is meant to be permanent. Try restarting your browser or use a another browser to check whether it worked.


1 Answers

You should be able to use the following RedirectMatch rule:

RedirectMatch 301 ^/products$ http://www.newsite.com/products

Note that this will only redirect requests for /products, not /products/, or /products/pagename.extension.

like image 98
waynethec Avatar answered Oct 15 '22 02:10

waynethec