How to add RewriteRules to Apache configuration files instead in the .htaccess file

Post Reply
User avatar
isscbta
Team Member
Posts: 130
Joined: Mon Jul 19, 2021 1:41 am
Has thanked: 15 times
Been thanked: 3 times

If you need to add RewriteRules in Apache configuration files, for instance, when you have multiple websites that point to the same public_html folder but require their own .htaccess file with unique rules, you can accomplish this easily using myVesta Hosting panel.

Simply add those rules into domain.conf file on this path: /home/someUser/conf/web/sapache2.domain.com.conf

It will work straight out of the box thanks to this code-line: https://github.com/myvesta/vesta/blob/3 ... 4.stpl#L41


For example:

Code: Select all

cd /home/someUser/conf/web
Let's create custom conf file:

Code: Select all

mcedit sapache2.mydomain.com.conf
Now copy-paste there this example of RewriteRules:

Code: Select all

RewriteEngine on

# always redirect to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# always redirect to www.
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Symlink https custom conf to http custom conf, in order to have the same rules for both http and https Apache template:

Code: Select all

ln -s sapache2.mydomain.com.conf apache2.mydomain.com.conf
At the end, restart Apache:

Code: Select all

systemctl restart apache2

Tags:
Post Reply