Search code examples
php.htaccesshttp-redirectmod-rewritesubdomain

Forcing www to a sub domain in .htaccess cause 500 internal server error


Suppose My domain is

  • example.com pointed to the server root (public_html)

I have created a sub domain

  • dev.example.com Pointed to the directory (public_html/development)

I wanted to always open my both site (main + subdomain) with www prefix

for that I place the below code to .htaccess

RewriteCond %{HTTP_HOST} !^www\.
#RewriteCond %{HTTP_HOST} !^dev\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Main site working fine its redirect domain.com to https://www.example.com

But dev.example.com redirects to https://www.dev.example.com/development and cause 500 Internal Server Error. It should be redirected to https://www.dev.example.com instead of https://www.dev.example.com/development

Where I am going wrong.

I tried different Codes in .htaccess but nothiing works.


Solution

  • Converting my comment into an answer to that it might help someone facing similar problem.

    Move above rule in development/.htaccess so that per directory directive RewriteRule sets $1 value relative to development directory instead of public_html directory.

    Make sure to test it after clearing browser cache.