.htaccess for either redirecting to subdomain or applying certain rules
Question by mrN
I am hosting a zend framework project on a subdomain. Lets say, project.mydomain.com
. This domain however can be accessed from mydomain.com/project
as well. Now, to avoid the complexity of having to maintain two different cases, I am trying to minimize the complexity by redirecting the directly accessed path to the subdomain, i.e.
mydomain.com/project > project.mydomain.com
but, if the same direcoty is accessed using subdomain, then i want some rules to be applied to it as well.
Here is my currect .htaccess which is allowing me to use direct path i.e mydomain.com/project/
RewriteEngine On
# Exclude some directories from URI rewriting
#RewriteRule ^(dir1|dir2|dir3) - [L]
RewriteRule ^.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /projects/myproject.com/public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /projects/myproject.com/public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /projects/myproject.com/public/index.php [NC,L]
Next, I will write a .htaccess for subdomain only. But, I want the directly accessed path to be redirected to the subdomain. How to do this redirection through .htaccess?
Please note, redirecting through a php file is not a option.
Answer by Starx
Robin, it looks like you are trying to redirect the path mydomain.com/projects
to projects.mydomain.com
. So, I dont think this is going to be combined .htaccess. As you would need, pretty much everything from /projects/your-anotherproject
to has to be redirected to projects.mydomain.com/your-anotherproject
. Rewriting htaccess for every projects is a BAD idea.
If this is the case, then i suggest having a simple .htaccess on projects/
root instead
RewriteEngine On
RedirectMatch 301 ^/projects/(.*)$ http://projects.mydomain.com/$1
This will solve you all cases, for all subdirectory, on that subdomain. Now write your project specific .htaccess
in the project’s root, like you are doing above