Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirect

url catch-all?

         

djvipernet

10:03 am on Feb 15, 2007 (gmt 0)

10+ Year Member



is there a way of redirecting users to a different directory, but whichever page/file they try to call it will send them to the index page of the new directory?

eg/
www.domain.com/subdir -> www.domain.com/newsubdir/index.php
www.domain.com/subdir/ -> www.domain.com/newsubdir/index.php
www.domain.com/subdir/page.php -> www.domain.com/newsubdir/index.php
www.domain.com/subdir/page.htm -> www.domain.com/newsubdir/index.php
www.domain.com/subdir/file.txt -> www.domain.com/newsubdir/index.php

so a catch-all for anything called from the subdir to redirect to the index page of the newsubdir.

my current .htaccess which resides in root contains the following:

**********
RewriteEngine on
Redirect permanent /subdir/ [domain.com...]
**********

which of course only captures the one specific requested url.

As you can probably tell i am fairly new to the htaccess stuff, so i am sorry if this is an obvious one.

paul.

phranque

10:47 am on Feb 15, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



greetings djv and welcome to WebmasterWorld!

try this:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/subdir /newsubdir/index.php [R]

note:
- this will also catch something like www.domain.com/subdirfoo
as well as something like www.domain.com/subdir/foodir/what.ever

- this will do a temporary external redirect.
use [R=301] for permanent redirect or drop the [R] for internal redirect.

you should check out the Library and Charter links for this forum - lot's of good resources there....

djvipernet

11:33 am on Feb 15, 2007 (gmt 0)

10+ Year Member



thankyou for the reply and welcome phranque :)

ok, I don't think my server likes ReWriteRule. Whenever i use that, it either gives me my 404 page or a 500 error, depending on what other stuff i have put in htaccess.

I have it almost working how i wish. i have the following in htaccess:

Options +FollowSymLinks
RewriteEngine on
Redirect permanent /subdir/ [domain.com...]

so going to /subdir/foo.htm takes me to /newsubdir/foo.htm

however i want all pages to redirect to the index page. so i updated htaccess to the following:

Options +FollowSymLinks
RewriteEngine on
Redirect permanent /subdir/ [domain.com...]

but then if i go to /subdir/foo.htm it redirects me to /newsubdir/index.phpfoo.htm

:(

am i missing something? it feels like it would be some small bit of code, just to say "ok, the directory is one we need to redirect, there is some kind of file/page request, but we'll send them to the index page of the new directory anyway".

phranque

12:29 pm on Feb 15, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you must use a directive that supports regular expression matching.

try this:

Options +FollowSymLinks
RewriteEngine on
RedirectMatch permanent ^/subdir.*$ http://www.example.com/newsubdir/index.php

note: "examplified"

djvipernet

12:40 pm on Feb 15, 2007 (gmt 0)

10+ Year Member



phranque, you are a superstar! :) that worked a treat, thank you.

I'm starting to like this .htaccess stuff hehe. hopefully i can get to grips with it a bit better eventually :)

again, thank you for your help!

paul.