Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

My website is built up like the following

www.domain.com/products/catname/myproduct.php

www.domain.com/products/myproduct.php

Some files are in deeper directories. I am wanting to redirect www.domain.com/products/catname/ (for example) to a certain page. My directories do not include index files.

How do I do this using htaccess?

I have tried

Options +FollowSymLinks
RewriteEngine On
RedirectMatch permanent ^/products http://domain.co.uk/
RedirectMatch permanent ^/products/productname http://domain.co.uk/

But this redirect any file in the /products directory (domain.co.uk/products/prod1.php for example will redirect to domain.co.uk)

share|improve this question

migrated from stackoverflow.com Oct 2 '12 at 22:03

This question came from our site for professional and enthusiast programmers.

    
what have you tried? –  Nelson Oct 2 '12 at 18:34
    
Options +FollowSymLinks RewriteEngine On RedirectMatch permanent ^/products domain.co.uk RedirectMatch permanent ^/products/productname domain.co.uk –  LCDesign Cambridge Oct 2 '12 at 18:37
    
This redirect the /products/product.php to the destination which it shouldnt. Only is someone browses to the directory root –  LCDesign Cambridge Oct 2 '12 at 18:38
    
Update your question with that, that way more people can help you. –  Nelson Oct 2 '12 at 18:38
    
I have updated it :-) –  LCDesign Cambridge Oct 2 '12 at 18:40
add comment

2 Answers 2

up vote 0 down vote accepted

Using mod_alias:

Options +FollowSymLinks
RewriteEngine On
RedirectMatch permanent ^/products/?(productname)?$ http://domain.co.uk/

Using mod_rewrite:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/?products/?(productname)?$ http://domain.co.uk/ [L,R=301]
share|improve this answer
    
Works a treat...just one thing.. So I have it redirected to tempdomain.com/page.php I am building the site on a demo server. So I will have to change each redirect link. Unless there is a way to pull the host address?? –  LCDesign Cambridge Oct 2 '12 at 19:34
    
@LCDesignCambridge not sure what you mean by having to change each redirect link. –  Jon Lin Oct 2 '12 at 20:30
    
@LCDesignCambridge Oh wait, do you mean you are redirecting to the same host that you are requesting /products/ from? If so, then simply remove the http://domain.co.uk part, and it will redirect to the same host. –  Jon Lin Oct 2 '12 at 20:31
    
great ok. If I need to go back a directory or two do I simply use ../ ? –  LCDesign Cambridge Oct 2 '12 at 20:50
    
@LCDesignCambridge ../ isn't guaranteed to work. If you're going to need to redirect to a parent directory, try: http://%{HTTP_HOST}/ –  Jon Lin Oct 2 '12 at 21:00
add comment

Try this:

Options +FollowSymLinks
RewriteEngine On
RedirectMatch permanent ^/products/[^/]+/ http://domain.co.uk/somepage.htm
RedirectMatch permanent ^/products http://domain.co.uk/

substitute 'somepage.htm' above for the actual page you want to redirect to.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.