Answer the question
In order to leave comments, you need to log in
Multi-domain and .htaccess?
Greetings. There is a site written in PHP, the site works with several domains, depending on the domain displays the desired content. In .htaccess, you need to include a set of rules that are used only for a specific domain. Is it possible to include rules from other files in .htaccess, something like:
Include %{HTTP_HOST}.conf?
Maybe there are some other ways?
It’s in my mind to write RewriteCond for each domain, but then .htaccess will be too voluminous.
Answer the question
In order to leave comments, you need to log in
Only through RewriteCond alas. At least there are fewer problems with it. Do you have VPS/Dedicated or shared hosting?
On my sites, this is solved at the stage of engine initialization, but the engine is self-written with support for multi-domain architecture. I think this is a more convenient solution than solving it with mod_rewrite
server {
listen 80;
server_name domain1.com domain2.com domain3.com;
charset utf-8;
location / {
root /home/www/domains;
index index.php index.html index.htm;
if (-f $request_filename) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^/photos/(.*)/(.*)$ /photos/domain1/$1/$2 break;
rewrite ^/photos/(.*)/(.*)$ /photos/domain1/$1/$2 break;
rewrite ^/photos/(.*)/(.*)$ /photos/domain3/$1/$2 break;
rewrite ^/([^/]*)/([^/]*)/$ /index.php?do=$1&type=$2 last;
rewrite ^/([^/]*).html/([^/]*)/$ /index.php?do=$1&page=$2 last;
rewrite ^/([^/]*)/([^/]*).html$ /index.php?do=$1&point=$2 last;
rewrite ^/([^/]*).html$ /index.php?do=$1 last;
rewrite ^/([^/]*)/$ /index.php?do=$1 last;
rewrite ^/sitemap.xml /sitemap.php last;
}
}
location ~ \.php$ {
root /home/www/domains;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/domains$fastcgi_script_name;
include fastcgi_params;
}
}
and in index.php
$host = $_SERVER['HTTP_HOST'];
$directory = realpath(dirname(__FILE__)).'/config/';
$dir = opendir($directory);
while(($file = readdir($dir))){
if (is_file($directory.$file) && preg_match('/'.$host.'/i', $file)){
include($directory.$ host.".php");
}
}
closedir($dir);
We connect the necessary config depending on the host.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question