How to remove subdirectory using rewriterule with non-www redirection

51 views Asked by At

So I've been fiddling with .htaccess and I was able to remove both removing a subdirectory and having a non-www subdirection. Please find code below:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*) http://domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(.*)subdir
RewriteRule ^(.*)$ subdir/$1 [L]

This can redirect:

www.domain.com/subdir/file

or

www.domain.com/file

to

domain.com/file

However, it can't redirect

domain.com/subdir/file

to

domain.com/file

That's my only problem with this code. Could somebody enlighten me what am I doing wrong? Any help is much appreciated. :)

1

There are 1 answers

5
anubhava On BEST ANSWER

You can use an OR condition in conditions:

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{THE_REQUEST} /subdir/ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(?:subdir/)?(.*)$ http://%1/$1 [NC,R=302,L]