Forum Moderators: phranque

Message Too Old, No Replies

htaccess to redirect all files in subdir to new domain

         

timware

5:48 am on Sep 9, 2014 (gmt 0)

10+ Year Member



I can't see why this doesn't work.

I have this in the .htaccess file in the doc root. I just replaced the static site with a WordPress site, and so need to redirect the many files in Google's index to a more appropriate domain. (There's no issue with duplicate directory names, btw.)

RedirectPermanent /mason-dixon/ http://www.example.com/wiki/index.php?title=Main_Page

I want to redirect any file within the "mason-dixon" directory to the home page of www.example.com/

the path of which is as above, although this works as well: http://www.example.com/wiki

I just can't get it to work. Any help greatly appreciated. Thanks!

[edited by: Ocean10000 at 1:10 pm (utc) on Sep 9, 2014]
[edit reason] Examplified [/edit]

timware

5:57 am on Sep 9, 2014 (gmt 0)

10+ Year Member



I should add that if you access this URL: http://example.com/mason-dixon/ the redirect works as it should.

But if you try to access any file within that directory, the redirect appends the file name to the target URL.

[edited by: Ocean10000 at 1:11 pm (utc) on Sep 9, 2014]
[edit reason] Examplified [/edit]

wilderness

12:39 pm on Sep 9, 2014 (gmt 0)

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



mod alias doesn't capture "strings", rather you need to use mod_rewrite.

FWIW, please use example.com for domain names [webmasterworld.com] per forum charter, and thread directly above yours.

timware

2:30 pm on Sep 9, 2014 (gmt 0)

10+ Year Member



Thanks, and sorry for the domain names! Could you provide an example for the above?

timware

2:51 pm on Sep 9, 2014 (gmt 0)

10+ Year Member



To be clear, I want to redirect all contents (files & directories) to the new domain home page. I can't seem to prevent the redirects, even using a mod_rewrite, from appending the name of the file to the target URL of the redirect.

wilderness

3:19 pm on Sep 9, 2014 (gmt 0)

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



this is the most frequently (and regularly) question asked in this forum.

search Webmaster World (just below top of page (SEARCH):

"mod alias" +string [google.com]

just review the results that have "apache" in the page address.

timware

3:36 pm on Sep 9, 2014 (gmt 0)

10+ Year Member



I ended up using the following and it does the trick:

RedirectMatch 301 ^/subdirectory/(.*) http://subdomain.differentdomain.com/wiki/

Cheers.

[edited by: phranque at 5:26 pm (utc) on Sep 9, 2014]
[edit reason] unlinked URL [/edit]

lucy24

3:58 pm on Sep 9, 2014 (gmt 0)

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



But if you try to access any file within that directory, the redirect appends the file name to the target URL.

If you don't want this to happen, why are you using mod_alias in its simple form (Redirect alone)? That's exactly how it is supposed to work. If you need to capture and/or throw away part of the path, you have to use RedirectMatch with a regular expression.

Here the question has become irrelevant because WP uses mod_rewrite. That means you need to take any existing mod_alias redirects and convert them to mod_rewrite syntax. These added redirects have to go before the standard WP block in your htaccess.