Forum Moderators: phranque

Message Too Old, No Replies

Best way to redirect an entire directory?

         

gnetcon

4:50 pm on Nov 28, 2008 (gmt 0)

10+ Year Member



Hello, all!

I'm in need of some quick guidance on the best way to redirect any traffic to a specific directory on a site to another page. I have a sub-directory on my server that needs to be redirected to another page, due to some needed changes.

Example:

/sub/index.php => /gone.php
/sub/view.php => /gone.php
/sub/print.php => /gone.php
/sub2/index.php => leave alone

Thanks in advance!

Samizdata

6:18 pm on Nov 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There are plenty of examples in this forum and a Search link at the top of the page.

If you still can't make it work, post your "best effort" code here.

...

gnetcon

7:32 pm on Nov 28, 2008 (gmt 0)

10+ Year Member



My apologies. I thought I had searched before, but not as thoroughly as I should have.

This is what I eventually came up with:

RewriteEngine on
RedirectMatch 301 ^/sub http://www.example.net/

Samizdata

7:51 pm on Nov 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You don't need to invoke RewriteEngine and your redirect will catch "sub2" directory requests.

RedirectMatch 301 ^/sub/ http://www.example.com/gone.php

Try putting that near the top of the .htaccess in your root directory.

...

g1smd

11:39 pm on Nov 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you have already used RewriteRule in your file somewhere else, don't start adding lines with Redirect or RedirectMatch in them.

They don't all play well together. Use RewriteRule for everything, rather than a mixture.

Samizdata

11:55 pm on Nov 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



They don't all play well together

Can you explain this a little more?

I know that there are many possible server configurations and that as the .htaccess file grows things can get complicated, and you mentioned in another thread that execution order may depend on the load order of modules specified in the httpd.conf file.

But across several shared hosts (and my local installations) I have never had a problem with such conflicts - in all cases I have Redirect and RedirectMatch calls at (or near) the top of the file and everything involving mod_rewrite below them.

Am I just lucky, or should I get out more?

...

g1smd

12:43 am on Nov 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The order of the rules in the file is important "per module" but the order of processing of rules from *different* modules is dependent on the reverse order of module loading in httpd.conf file.

I have seen jdMorgan warn of this so many times now, that it is always on my mind on any job involving any sort of redirects and/or rewrites. Just because it appears to work OK now, doesn't mean that it won't keel over tomorrow when your host upgrades Apache by a point release and makes minor (to them) changes to the configuration. :-)

g1smd

1:02 am on Nov 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Recent example of something breaking on Apache upgrade ... [webmasterworld.com...] ... and another ... [webmasterworld.com...] ... and another ... [webmasterworld.com...] ...

You might also be interested in [webmasterworld.com...] especially posts #3790261 and #3790916 too.

The second post by jdMorgan in this thread is useful [webmasterworld.com...] - post #3790425 that is.

Samizdata

1:14 am on Nov 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Many thanks, at least I will know what to look for when it all goes pear-shaped.

I have noticed a few minor configuration discrepancies between the hosts I use, but over the years I have evolved .htaccess techniques that work on all of them by trial and error.

Plenty of trial, and plenty of error :)

...

g1smd

1:28 am on Nov 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well you know what they say about "Error 500" don't you?

... only 499 to go. That's what jd says anyway.