Programming Maniac
Programming Maniac

Reputation: 127

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.

Upvotes: 1

Views: 733

Answers (2)

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

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

Upvotes: 1

Jigius
Jigius

Reputation: 315

Try this

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

Upvotes: 0

Related Questions