CunruiLi
CunruiLi

Reputation: 493

.htaccess redirect folder to a url

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.

Upvotes: 37

Views: 122844

Answers (4)

Kyle Coots
Kyle Coots

Reputation: 2131

I perfer the following method:

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

Upvotes: 2

Kevin Danikowski
Kevin Danikowski

Reputation: 5186

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]

Upvotes: 1

KnightHawk0811
KnightHawk0811

Reputation: 931

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/

Upvotes: 11

Jon Lin
Jon Lin

Reputation: 143866

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]

Upvotes: 46

Related Questions