I have two domains having same document root. As per the requirement, I want both the domains should have their own .htaccess file. Is their any way with whom I can achieve this?
Asked
Active
Viewed 6,626 times
2
-
1You can add a rule based on a condition of domain. – Bhavin Solanki Sep 06 '18 at 07:07
-
2This is obviously _not_ possible, since such dynamic configuration files are located and considered based on their location in the file system. What you _can_ do is: place the content (rewriting rules and stuff) in the real http servers host configuration which should be preferred anyway or you use conditions inside the file to decide what rules to apply based on the requested http host. – arkascha Sep 06 '18 at 07:09
-
Thank you everyone but my redirect rules are increasing day by day and it is becoming difficult for me to manage from the same .htaccess files. – Mia Sep 06 '18 at 07:22
-
Why do you insist on `.htaccess`? Configuration directives can be in the core Apache configuration files where you are free to structure it the way you need, and apply different directives per `VirtualHost` irrespective of the `DocumentRoot` sharing or not. – Patrick Mevzek Sep 06 '18 at 14:31
2 Answers
3
Below example contain two domain and we have to select query parameter based on the domain.
RewriteCond %{HTTP_HOST} ^www\.site1\.example [NC]
RewriteRule ^(.*)$ index.php?domainname=site1 [NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.site2\.example [NC]
RewriteRule ^(.*)$ index.php?domainname=site2 [NC,QSA]
But it's not possible to add more than one .htaccess
file in the same folder.

Patrick Mevzek
- 10,995
- 16
- 38
- 54

Bhavin Solanki
- 1,364
- 11
- 27
0
There's more options than just the common RewriteCond
masking. In particular if you need more than mod_rewrite directives, it can make sense to use:
One
AcesssFilename
per virtual host.<VirtualHost *:80> AccessFileName .htaccess2
Or simply an
<If>
section to split up one .htaccess:<If "%{HTTP_HOST} =~ 'www2'"> Include .htaccess2 # or a bunch of RewriteRules here </If>
For core directives, I would also advise to rather set those in the virtual host configuration itself. But for basic redirects a secondary .htaccess.domain2
might do.

mario
- 144,265
- 20
- 237
- 291
-
Thanks Mario, If I go with second option you have specified, then what will happen to those redirects which will be added through cPanel. Where will those rules be placed? – Mia Sep 06 '18 at 08:04
-
I'm pretty sure cPanel does not support any `
` sections nor secondary .htaccess files. You'll have to copy them over manually. Such GUI editors might even screw things up. Btw, if you have so many rules, then look into a [RewriteMap](https://httpd.apache.org/docs/current/rewrite/rewritemap.html) alternatively (either with text files or rules in a SQL database). – mario Sep 06 '18 at 08:17