简体   繁体   中英

Redirect domain https://domain.com to https://www.domain.com without rewrite rules just redirect in apache

I'm trying to redirect from https://domain.com to https://www.domain.com ,

I can achieve the following task with rewrite, but only want it via redirect 301.

VirtualHost domain.com:80 
RewriteEngine on 
ReWriteCond %{SERVER_PORT} !^443$ 
RewriteRule ^/?(.*) https://%{HTTP_HOST}/$1 [NC,R,L] 
VirtualHost _default_:443 
ServerAdmin webmaster@localhost 
ServerName domain.com:443 
ServerAlias www.domain.com 
DocumentRoot /var/www/html 
RewriteEngine on 
RewriteCond %{HTTP_HOST} !^(www\.)?(.+) 
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]

Redirect is not working for me as I tried to redirect / to https:// but its redirecting to https://domain.com not https://www.domain.com

You should not use mod-alias (Redirect) for this. Rewrite is more powerful,easier and the most common way to redirect based on conditions. Redirect will handle directories but doesn't have any conditions. Maybe you should try your rewrite rule this way instead. And with Rewrite you are doing a 301 , that is what the flag R=301 means.

RewriteEngine on 
RewriteCond %{HTTP_HOST} !^(www\.)
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM