brentonstrine
brentonstrine

Reputation: 22752

Trying to redirect a certain directory to a filename

I want to intercept HTTP requests for two specific directories:

/mydir/
/thisdir/

Whenever someone requests one of those two, they should be served /mydir.html and /thisdir.html, respectively. All other directories should behave as normal.

I can get it to work for all directories:

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/ $1.html [QSA]

But my attempts to get it to work just for the two particular directories:

RewriteCond %{REQUEST_FILENAME} ^/(mydir|thisdir)/$
RewriteRule ^(.+)/ $1.html [QSA]

or

RewriteCond %{REQUEST_URI} ^/(mydir|thisdir)/$
RewriteRule ^(.+)/ $1.html [QSA]

result in the directory index being displayed.

Upvotes: 0

Views: 52

Answers (2)

anubhava
anubhava

Reputation: 785256

You can use this rule:

RewriteCond %{THE_REQUEST} /(mydir|thisdir) [NC]
RewriteRule ^(mydir|thisdir)/?$ $1.html [L,NC]

Upvotes: 2

slash
slash

Reputation: 603

How about this ?

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(mydir|thisdir)$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI}$1.html [R=301]

Upvotes: 0

Related Questions