简体   繁体   中英

Redirect subdomains + http to https using mod_rewrite in Apache Virtual Host

I have a virtual host with 1 name and 2 alias, name.domain.com.br , alias1.domain.com.br and alias2.domain.com.br . I need to redirect alias 1 and 2 to name , but it's not working completely, this is my current code:

ServerName name.domain.com.br
ServerAlias alias1.domain.com.br
ServerAlias alias2.domain.com.br
DocumentRoot ****************
ErrorLog ****************
CustomLog ****************
RewriteEngine on
RewriteCond %{SERVER_NAME} =alias1.domain.com.br [OR]
RewriteCond %{SERVER_NAME} =alias2.domain.com.br [OR]
RewriteRule ^/(.*)$ https://name.domain.com.br [L,R=301]

However, this way the redirecting is only working when using http://aliasX.domain.com.br and for some reason it puts two slashes at the end of the link, getting https://name.domain.com.br// . It doesn't work when https:// is used.

I want all variations with or without https to redirect to https://name.domain.com.br .

can you help me?

 RewriteCond %{SERVER_NAME} =alias1.domain.com.br [OR] RewriteCond %{SERVER_NAME} =alias2.domain.com.br [OR] RewriteRule ^/(.*)$ https://name.domain.com.br [L,R=301]

It doesn't work when "https://" is used.

HTTP and HTTPS require two separate virtual host containers.

No doubt you've put these rules in the virtual host for HTTP only, eg. <VirtualHost *:80> . You would need to repeat the redirect in the virtual host for HTTPS as well. eg. <VirtualHost *:443> . (Although the redirect in the HTTP vHost can be simplified, since you just need to redirect everything to HTTPS - see my solution below.)

HOWEVER, the rule you've posted is incorrect. Due to the erroneous OR flag on the last condition ( RewriteCond directive), the redirect will always occur, effectively unconditionally .

You are also discarding the URL-path and redirecting to the homepage? (The browser corrects the missing slash.) I assume this is an error and you should be redirecting alias1.domain.com.br/foo to name.domain.com.br/foo , not name.domain.com.br/ ?

for some reason it puts two slashes at the end of the link

That is not caused by this rule. Something else must be doing that.

So, the solution then becomes...

###
### HTTP (not secure)
###
<VirtualHost *:80>
ServerName name.domain.com.br
ServerAlias alias1.domain.com.br
ServerAlias alias2.domain.com.br
DocumentRoot ****************
ErrorLog ****************
CustomLog ****************

# Redirect everything to HTTPS and canonical domain
# No need for mod_rewrite, a simple mod_alias Redirect will suffice
Redirect 301 / https://name.domain.com.br/
</VirualHost>

###
### HTTPS (secure)
###
<VirtualHost *:443>
ServerName name.domain.com.br
ServerAlias alias1.domain.com.br
ServerAlias alias2.domain.com.br
DocumentRoot ****************
ErrorLog ****************
CustomLog ****************

# Other SSL directives here...

# Redirect to the canonical domain
RewriteEngine On
RewriteCond %{HTTP_HOST} !=name.domain.com.br [NC]
RewriteRule ^ https://name.domain.com.br%{REQUEST_URI} [R=301,L]
</VirualHost>

The redirect in the HTTPS vHost checks that the request is not already for the canonical hostname, rather than checking that it is one of the aliases.

NB: When checking the requested hostname, use HTTP_HOST instead of SERVER_NAME . By default they are the same, however, depending on the server config, SERVER_NAME might reference the value of the ServerName directive, not the hostname the client requested.

You will need to clear your browser cache is cleared before testing, since any erroneous 301 (permanent) redirects will have been cached. Test first with 302 (temporary) redirects to avoid potential caching issues.

And as mentioned above, if you are still seeing a double slash at the start of the URL-path then something else is doing that.

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