36

I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.

But

Redirect 301 /abc/cba/ http://www.aaa.com/

Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html

What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/

Could anyone help? Thanks. If anything not clear, please let me know.

CunruiLi
  • 483
  • 1
  • 6
  • 16
  • I have my shared hosting server on godaddy.com and primary domain is abc.com Now I have another domain registered xyz.com on some other 3rd service not on godaddy. For this new website xyz.com I have installed wordpress in xyz directory of my godaddy hosting server. Now how do I point this xyz.com domain to my xyz directory of my hosting server by keeping abc.com website as it is? – Ruchir Shah Nov 07 '16 at 17:23

4 Answers4

44

By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.

Try:

RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?

Or if you'd rather use mod_rewrite instead of mod_alias:

RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • 6
    Just a hint: You should be really sure if you want to use `301`. Because it really can be a real **permanent** redirect ( https://stackoverflow.com/questions/9130422/how-long-do-browsers-cache-http-301s ). If you are not sure and want to avoid trouble in future, use `302`. Anyway advantageous in the implementation and testing phase. – Martin Schneider Jun 05 '18 at 21:04
  • Does case mater? Would `/abc/CBA/` get redirected to `http://www.aaa.com/?` with the provided solution or do you need to account for casing options, too? Also, what does the `?` do for us? – HPWD Aug 30 '21 at 14:58
10

here's another example of a mod_rewrite rule that worked for me

I wanted to redirect a sub directory to the root of the same domain.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>

more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/

KnightHawk
  • 901
  • 9
  • 18
2

I perfer the following method:

 RewriteEngine on
 RewriteCond %{REQUEST_URI}  ^/somedir           [NC]
 RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
Kyle Coots
  • 2,041
  • 1
  • 18
  • 24
1

I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html

RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]

What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .

It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html

I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.

RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]
Kevin Danikowski
  • 4,620
  • 6
  • 41
  • 75