简体   繁体   中英

htaccess redirect directory but not files in directory

I need to redirect from:

http://example.com/folder

to http://example.com/

But leave:

http://example.com/folder/file

Is this possible?

I have seen this question but it only works with subfolders and not files.

Based in your question ,There are two scenarios and it is up to you to choose the suitable one :

First:

RewriteEngine On 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/folder/?\sHTTP.*$
RewriteRule ^(.*)$    / [R=302,L]
RewriteRule ^$    /folder/ [L]

You catch the both requests http://example.com/folder or http://example.com/folder/ then redirect any one of them externally to http://example.com/ but still get the data internally from that folder.

Second:

RewriteEngine On 
RewriteCond %{REQUEST_URI} /folder/?$
RewriteRule ^(.*)$    /  [R=302,L]

You catch the both requests http://example.com/folder or http://example.com/folder/ then redirect any one of them to http://example.com/ .

with these two scenarios any request to http://example.com/folder/file will be as is.

Note: if any one is ok , change R=302 to R=301 to be permanent redirection

Try this

RewriteCond %{REQUEST_URI} ^/folder/?$
RewriteCond %{REQUEST_URI} !^/folder/file$
RewriteRule (.*) / [R=301,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM