If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Multi - domains and .htaccess

Started by commodity, Apr 13, 2023, 06:14 AM

Previous topic - Next topic

commodityTopic starter

Hello there.
We have a PHP website that functions on multiple domains, displaying relevant content based on the domain. We need to add a specific set of rules to .htaccess file for one of the domains.

Is it feasible to incorporate rules from other files into .htaccess using something like "Include %{HTTP_HOST}.conf"?
Are there any alternative methods? I was considering writing individual RewriteCond statements for each domain, but that would make .htaccess file quite large.
  •  


Deepak1

Can you specify if you're using VPS/Dedicated or shared hosting and if your domains are separate or subdomains of a single domain?
Also, it's fortunate that there are fewer issues with RewriteCond.
  •  

Magir

I solve this issue on my websites by initializing the engine, which is a self-written one with support for multi-domain architecture. I think this approach is more convenient than using mod_rewrite. If you have a VPS and Nginx, then in index.php, you can use the code snippet provided to connect the necessary configuration based on the host:

$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);
  •  

Elev8

1) Configure the server settings by setting the parameter for the server protocol to https in the system settings.
2) Use the .htaccess file to redirect all requests to https by adding the following lines:

RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
  •  

plegiorotte

While the idea of using "Include" directive in .htaccess to dynamically include rules based on the HTTP_HOST variable is conceptually appealing, it's important to note that this is not a feature supported in the Apache server configuration. The "Include" directive in .htaccess is designed to include static files, not to dynamically generate or include files based on runtime variables such as the HTTP_HOST.

The approach of creating individual .htaccess files for each domain can be effective to ensure specific rules for each domain are maintained separately. However, this might result in a degree of duplicative code if the rules overlap across domains, potentially making future updates or rule modifications more cumbersome to manage.

Using a single .htaccess file with RewriteCond statements for each domain offers a centralized solution for managing rules. While it may result in a larger .htaccess file, organizing the rules with clear comments and logical groupings can improve the manageability of the file. Using this approach also allows for a comprehensive overview of the domain-specific rules within a single file, potentially simplifying maintenance in certain scenarios.

Another consideration is handling the domain-specific logic within the PHP code instead of the .htaccess file. By leveraging the $_SERVER['HTTP_HOST'] variable, PHP can dynamically process domain-specific rules and configurations. This approach offers flexibility and programmatically manages domain-specific rules within the application code, potentially simplifying maintenance and providing the ability to interact with dynamic content based on the current domain.
As the website evolves, the chosen approach should accommodate potential changes, additions, and updates to domain-specific rules while maintaining clarity and manageability. Additionally, consider how each approach aligns with the website's specific requirements and the development team's expertise to ensure seamless implementation and maintenance.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...