Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

.htaccess - Apache servers redirect to one specific server

Is there any solution to redirect all these urls to one url without creating a specific server and then redirect it from that server to server I want. I was wondering to do that with .htaccess but since that server doesn't exist then how I can execute htaccess there...

http://example.com  -----|
http://www.example.com --|-------- to: https://www.example.com
https://example.com -----|

.conf file

<VirtualHost *:80>
    ServerName example.com
    Redirect / https://www.example.com
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
    Redirect / https://www.example.com
</VirtualHost>

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        DocumentRoot /var/www/example.com
        ServerName www.example.com

        SSLEngine on
        SSLCertificateFile       ...
        SSLCertificateKeyFile    ...

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
</IfModule>
question from:https://stackoverflow.com/questions/65889641/apache-servers-redirect-to-one-specific-server

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your htaccess shoud look like this:

RewriteCond %{HTTP_HOST} !^www.example.com
RewriteRule ^(.*)$ https://www.example.com/$1 [R=permanent,L]

RewriteCond %{HTTPS}  !=on 
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L] 

Configuration might need to be changed according to how your network is setup, for example if you have a load-balancer in front of your apache or according to the apache version you're running.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...