Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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.

like image 911
CunruiLi Avatar asked Jul 30 '12 05:07

CunruiLi


People also ask

How do I redirect a folder?

Right-click a folder that you want to redirect (for example, Documents), and then select Properties. In the Properties dialog box, from the Setting box, select Basic - Redirect everyone's folder to the same location.

How do I redirect one link to another link in htaccess?

Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.

How do I redirect a URL to a subdirectory?

Redirect "ALL" requests to a domain to a subdirectory You can redirect all requests to a subdirectory by adding an . htaccess file to the root of your domain's directory: Visit the FTP page for instructions on how to upload.


2 Answers

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] 
like image 78
Jon Lin Avatar answered Oct 05 '22 10:10

Jon Lin


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/

like image 30
KnightHawk Avatar answered Oct 05 '22 11:10

KnightHawk