How do I write .htaccess to permanently redirect multiple folders to subdomain with working query string?

246 Views Asked by At

I need to use .htaccess to permanently redirect domain.com/folder to folder.domain.com
I also need to convert domain.com/folder and folder.domain.com/folder2 to folder.domain.com/index.php?source=folder2
index.php being hidden... so it ends up as folder.domain.com/?source=folder2
This needs to work whether or not there is a trailing slash
The ?source=folder2 needs to actually work as a query string for $_GET

1

There are 1 best solutions below

1
On

Try adding this to the domain.com document root's htaccess file:

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

And in the htaccess file for folder.domain.com (of if it's the same, just append it):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^folder\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?source=$1 [L]