Hosting & Domaining Forum

Domain Marketplace => Websites For Sale => External Domain and Website Sales => Topic started by: commodity on Apr 13, 2023, 06:14 AM

Title: Multi - domains and .htaccess
Post by: commodity on Apr 13, 2023, 06:14 AM
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.
Title: Re: Multi - domains and .htaccess
Post by: Deepak1 on Apr 13, 2023, 08:31 AM
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.
Title: Re: Multi - domains and .htaccess
Post by: Magir on Apr 13, 2023, 10:06 AM
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);
Title: Re: Multi - domains and .htaccess
Post by: Elev8 on Apr 13, 2023, 12:18 PM
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]
Title: Re: Multi - domains and .htaccess
Post by: plegiorotte on Apr 20, 2024, 12:59 AM
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.