Score:0

How to redirect subdomain in apache virtualhost (CentOS8)?

ps flag

I have an apache virtual host for the main domain and subdomain as below.

<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /var/www/html/example/public 
<Directory /var/www/html/example> 
  AllowOverride All 
</Directory> 
RewriteEngine On 
RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [R=301,L] 
</VirtualHost>

<VirtualHost *:80>
  ServerName subdomain.example.com
  DocumentRoot /var/www/html/example/public 
<Directory /var/www/html/example> 
  AllowOverride All 
</Directory> 
RewriteEngine On 
RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [R=301,L] 
</VirtualHost>

Both call the same folder but redirect different routes in the programming level. I have a problem that the subdomain always redirects to the main domain.

diya avatar
la flag
A simple redirect HTTP ==> HTTPS is a text book example when not to use mod_rewrite https://httpd.apache.org/docs/2.4/rewrite/avoid.html . Note that additionally ***permanent redirects get cached*** by your browser, so changing a configuration requires testing in a new/fresh anonymous/incognito browser window or clearing your caches before you can see the effect.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.