Take the 2-minute tour ×

My URL structure is currently as follows:

http://domain.com/folder/filename (CURRENT)

I want to change this so that I can use the following URL instead:

http://sub.domain.com/filename (NEW)

So accessing the CURRENT or the NEW url, should load the file located at the CURRENT url, but show the NEW url in the address bar. It should only apply to the "/folder/" path.

sub.domain.com is a mirror of domain.com, ie. they share the same file system and root directory.

This is what I have so far:

Options +FollowSymLinks
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder/?(.*)$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]

This is working, but is missing the rule to remove the "/folder/" from the path. I've tried combining multiple RewriteRule's with no luck. Any ideas? Thanks.

UPDATE: Thanks again @Gerben - I understand what your rules are doing now, but the second one isn't working for me. I suspect because it's conflicting with some other rewrite rules, in particular those of WordPress, which are lower down in my .htaccess file:

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

Because of this the page ends up in a redirect loop, ie (from Chrome): "The webpage at http://sub.domain.com/folder/index.php has resulted in too many redirects." - while the url I was originally trying to access was, for example, http://sub.domain.com/page

Any ideas?

share|improve this question
RewriteRule ^folder/(.*)$ http://sub.domain.com/$1 [R=301,L] –  Gerben Feb 26 '12 at 19:35
Gerben - thanks, but like @jon-lin's answer below, this is also resulting in a 404, as the file doesn't physically exist at sub.domain.com - but I want it to appear that it does. If .htaccess rewriting is not the correct solution for this problem then I'm open to alternative ideas. –  Damien Feb 28 '12 at 13:02

2 Answers 2


RewriteRule ^(folder/)?(.*)$ http://sub.domain.com/$2 [R=301,L]

This will redirect everything to sub.domain.com, and remove the /folder part of the URI if it is there. If not, it redirects and leaves the URI untouched.

share|improve this answer
Thanks @jon-lin. This is only partly working. The redirect is working, ie. "domain.com/folder/filename"; (CURRENT) redirects to "sub.domain.com/filename"; (NEW). But this results in a 404, since that file at the NEW url doesn't exist. The file at the CURRENT url hasn't moved. It's more of a URL rewrite than a physical redirect that I need. Typing the NEW url in the browser should load the page at the CURRENT url, without changing the url in the address bar. –  Damien Feb 28 '12 at 12:56
RewriteCond %{THE_REQUEST} /folder/

RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]

# WordPress rules here

edit the second R=301 should not have been there

But this won't work, as wordpress has no way of knowing you want folder. You could add the Proxy flag to the rewrite, but then you need to change the rule above to not redirect on this internal proxy request.

share|improve this answer
Thanks, I've replied below (couldn't include required formatting in a comment) –  Damien Feb 29 '12 at 2:39
I assume folder is not part of WP. See edit above. –  Gerben Feb 29 '12 at 11:54
Yes folder is part of WP: a parent page. –  Damien Feb 29 '12 at 12:32
See edit above. –  Gerben Feb 29 '12 at 16:26
Thanks, how would I go about that (change to rule to not redirect on the internal proxy) ? This is driving me insane so whatever works! –  Damien Feb 29 '12 at 22:00

Your Answer


