user3037561
user3037561

Reputation: 271

.htaccess preserve url on rewrite

I am redirecting a domain to a sub folder with the following

RewriteCond %{HTTP_HOST} ^domain.net [OR]
RewriteCond %{HTTP_HOST} ^www.domain.net$
RewriteCond %{REQUEST_URI} !subfolder/
RewriteRule ^(.*)$ subfolder/$1 [L]

however if I visit domain.net/folderinsidethatsub

the url is rewritten to

domain/net/subfolder/folderinsidethatsub

Is there any way to prevent this?

Upvotes: 0

Views: 56

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

This is probably because mod_dir redirects any request for a directory without a trailing slash to the same URL but with a trailing slash. There's a VERY GOOD REASON for this because it prevents the contents of the folder from being exposed (even if you have an index file). The only way to keep mod_dir from doing this is to turn off the DirectorySlash directive, however, you'll need mod_rewrite rules to add the trailing slash manually, otherwise people will be able to expose the contents of your directories. Add this above the rules that you already have:

DirectorySlash Off

RewriteCond %{DOCUMENT_ROOT}/subfolder%{REQUEST_URI} -d
RewriteRule ^(.+[^/])$ /$1/ [L,R=301]

Upvotes: 1

Related Questions