best redirect to www

Ondra3211

Contributor
May 5, 2018
65
37
111
Hi, Does anyone know how to make the best redirects to www?

I have Apache2 and I need it this:
Code:
http://domain.tld -> https://www.domain.tld
https://domain.tld -> https://www.domain.tld
http://www.domain.tld -> https://www.domain.tld

www-domain config
Apache config:
<VirtualHost *:80>
    <Directory "/var/www/html">
    AllowOverride All
    </Directory>
    ServerName www.******.eu
    ServerAdmin ******@*****.eu
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.******.eu
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
domain config
Apache config:
<VirtualHost *:80>
    <Directory "/var/www/html">
    AllowOverride All
    </Directory>
    ServerName ******.eu
    ServerAdmin ******@******.eu
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
 

MrWolf

Retired Staff
Contributor
Dec 27, 2016
475
263
112
Try this, paste it into .htaccess file.
Code:
RewriteCond %{HTTP_HOST} ^yoursite.com$ [NC]
RewriteRule (.*) https://www.yoursite.com/$1 [R=301,L]
 
E

ewenjo

You could also do something like this

Apache config:
<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / http://www.example.com/
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
    # real server configuration
</VirtualHost>
 

NNE

C++ priest
VIP
Jul 20, 2016
21
15
78
Apache config:
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTP_HOST} !=www.yourhost.fld
RewriteRule ^(.*)$ https://www.yourhost.fld/$1 [R=permanent,L]

Also you can add SSLRequireSSL flag to disable access through unsecured http at all.
 
Last edited:

BIOS

Knowledge Seeker
VIP
Oct 10, 2015
447
848
208
its my config

Code:
#ATTENTION!
#
#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.
#IF YOU REQUIRE TO APPLY CUSTOM MODIFICATIONS, PERFORM THEM IN THE FOLLOWING FILES:
#/var/www/vhosts/system/bio-test.ovh/conf/vhost.conf
#/var/www/vhosts/system/bio-test.ovh/conf/vhost_ssl.conf
<IfModule mod_ssl.c>

    <VirtualHost IPADRESS:443 >
        ServerName "bio-test.ovh:443"
        ServerAlias "www.bio-test.ovh"
        ServerAlias "ipv4.bio-test.ovh"
        UseCanonicalName Off

        DocumentRoot "/var/www/vhosts/bio-test.ovh/httpdocs"
        CustomLog /var/www/vhosts/system/bio-test.ovh/logs/access_ssl_log plesklog
        ErrorLog "/var/www/vhosts/system/bio-test.ovh/logs/error_log"

        <IfModule mod_suexec.c>
            SuexecUserGroup "ene" "psacln"
        </IfModule>

        <IfModule mod_userdir.c>

            UserDir "/var/www/vhosts/bio-test.ovh/web_users/*"
        </IfModule>

        <IfModule mod_sysenv.c>
            SetSysEnv PP_VHOST_ID "4fab946a-1420-4f51-aa98-d7f02f64f4db"
        </IfModule>

        ScriptAlias "/cgi-bin/" "/var/www/vhosts/bio-test.ovh/httpdocs/cgi-bin/"

        Alias "/plesk-stat" "/var/www/vhosts/system/bio-test.ovh/statistics"
        <Location  /plesk-stat/>
            Options +Indexes
        </Location>
        <Location  /plesk-stat/logs/>
            Require valid-user
        </Location>
        Alias /webstat /var/www/vhosts/system/bio-test.ovh/statistics/webstat
        Alias /webstat-ssl /var/www/vhosts/system/bio-test.ovh/statistics/webstat-ssl
        Alias /ftpstat /var/www/vhosts/system/bio-test.ovh/statistics/ftpstat
        Alias /anon_ftpstat /var/www/vhosts/system/bio-test.ovh/statistics/anon_ftpstat
        Alias /awstats-icon /usr/share/awstats/icon

        SSLEngine on
        SSLVerifyClient none
        SSLCertificateFile /opt/psa/var/certificates/scf6fGsxD
        SSLCACertificateFile /opt/psa/var/certificates/scfpDqDgz

        <IfModule mod_fcgid.c>
            FcgidInitialEnv PP_CUSTOM_PHP_INI /var/www/vhosts/system/bio-test.ovh/etc/php.ini
            FcgidInitialEnv PP_CUSTOM_PHP_CGI_INDEX plesk-php70-fastcgi
            FcgidMaxRequestLen 134217728
        </IfModule>

        <Directory /var/www/vhosts/bio-test.ovh/httpdocs>

            <IfModule mod_fcgid.c>
                <Files ~ (\.fcgi$)>
                    SetHandler fcgid-script
                    Options +ExecCGI
                </Files>
            </IfModule>
            <IfModule mod_fcgid.c>
                <Files ~ (\.php$)>
                    SetHandler fcgid-script
                    FCGIWrapper /var/www/cgi-bin/cgi_wrapper/cgi_wrapper .php
                    Options +ExecCGI
                </Files>
            </IfModule>

            SSLRequireSSL

            Options -Includes +ExecCGI

        </Directory>

        <Directory "/var/www/vhosts/system/bio-test.ovh/statistics">
            AuthType Basic
            AuthName "Domainstatistiken"
            AuthUserFile "/var/www/vhosts/system/bio-test.ovh/pd/d..httpdocs@plesk-stat"
            require valid-user
        </Directory>

        Alias /error_docs /var/www/vhosts/bio-test.ovh/error_docs
        ErrorDocument 400 /error_docs/bad_request.html
        ErrorDocument 401 /error_docs/unauthorized.html
        ErrorDocument 403 /error_docs/forbidden.html
        ErrorDocument 404 /error_docs/not_found.html
        ErrorDocument 500 /error_docs/internal_server_error.html
        ErrorDocument 405 /error_docs/method_not_allowed.html
        ErrorDocument 406 /error_docs/not_acceptable.html
        ErrorDocument 407 /error_docs/proxy_authentication_required.html
        ErrorDocument 412 /error_docs/precondition_failed.html
        ErrorDocument 414 /error_docs/request_uri_too_long.html
        ErrorDocument 415 /error_docs/unsupported_media_type.html
        ErrorDocument 501 /error_docs/not_implemented.html
        ErrorDocument 502 /error_docs/bad_gateway.html
        ErrorDocument 503 /error_docs/maintenance.html

        DirectoryIndex "index.html" "index.cgi" "index.pl" "index.php" "index.xhtml" "index.htm" "index.shtml"

        <Directory /var/www/vhosts/bio-test.ovh>
            AllowOverride AuthConfig FileInfo Indexes Limit Options=Indexes,SymLinksIfOwnerMatch,MultiViews,FollowSymLinks,ExecCGI,Includes,IncludesNOEXEC
        </Directory>

        <IfModule mod_headers.c>
        </IfModule>

        #extension letsencrypt begin
        Alias /.well-known/acme-challenge "/var/www/vhosts/default/htdocs/.well-known/acme-challenge"

        <Location /.well-known/acme-challenge/>
            # Require all granted
            Order Deny,Allow
            Allow from all
            Satisfy any
        </Location>

        <LocationMatch "^/.well-known/acme-challenge/(.*/|)\.">
            # Require all denied
            Order Allow,Deny
            Deny from all
        </LocationMatch>
        #extension letsencrypt end
    </VirtualHost>

</IfModule>

<VirtualHost IPADRESS:80 >
    ServerName "bio-test.ovh:80"
    ServerAlias "www.bio-test.ovh"
    ServerAlias "ipv4.bio-test.ovh"
    UseCanonicalName Off

    DocumentRoot "/var/www/vhosts/bio-test.ovh/httpdocs"
    CustomLog /var/www/vhosts/system/bio-test.ovh/logs/access_log plesklog
    ErrorLog "/var/www/vhosts/system/bio-test.ovh/logs/error_log"

    <IfModule mod_suexec.c>
        SuexecUserGroup "ene" "psacln"
    </IfModule>

    <IfModule mod_userdir.c>

        UserDir "/var/www/vhosts/bio-test.ovh/web_users/*"
    </IfModule>

    <IfModule mod_sysenv.c>
        SetSysEnv PP_VHOST_ID "4fab946a-1420-4f51-aa98-d7f02f64f4db"
    </IfModule>

    ScriptAlias "/cgi-bin/" "/var/www/vhosts/bio-test.ovh/httpdocs/cgi-bin/"

    Redirect permanent /plesk-stat https://bio-test.ovh/plesk-stat
    Redirect permanent /webstat https://bio-test.ovh/webstat
    Redirect permanent /webstat-ssl https://bio-test.ovh/webstat-ssl
    Redirect permanent /ftpstat https://bio-test.ovh/ftpstat
    Redirect permanent /anon_ftpstat https://bio-test.ovh/anon_ftpstat
    Redirect permanent /awstats-icon https://bio-test.ovh/awstats-icon

    <IfModule mod_ssl.c>
        SSLEngine off
    </IfModule>

    <IfModule mod_fcgid.c>
        FcgidInitialEnv PP_CUSTOM_PHP_INI /var/www/vhosts/system/bio-test.ovh/etc/php.ini
        FcgidInitialEnv PP_CUSTOM_PHP_CGI_INDEX plesk-php70-fastcgi
        FcgidMaxRequestLen 134217728
    </IfModule>

    <Directory /var/www/vhosts/bio-test.ovh/httpdocs>

        <IfModule mod_fcgid.c>
            <Files ~ (\.fcgi$)>
                SetHandler fcgid-script
                Options +ExecCGI
            </Files>
        </IfModule>
        <IfModule mod_fcgid.c>
            <Files ~ (\.php$)>
                SetHandler fcgid-script
                FCGIWrapper /var/www/cgi-bin/cgi_wrapper/cgi_wrapper .php
                Options +ExecCGI
            </Files>
        </IfModule>

        Options -Includes +ExecCGI

    </Directory>

    <Directory "/var/www/vhosts/system/bio-test.ovh/statistics">
        AuthType Basic
        AuthName "Domainstatistiken"
        AuthUserFile "/var/www/vhosts/system/bio-test.ovh/pd/d..httpdocs@plesk-stat"
        require valid-user
    </Directory>

    Alias /error_docs /var/www/vhosts/bio-test.ovh/error_docs
    ErrorDocument 400 /error_docs/bad_request.html
    ErrorDocument 401 /error_docs/unauthorized.html
    ErrorDocument 403 /error_docs/forbidden.html
    ErrorDocument 404 /error_docs/not_found.html
    ErrorDocument 500 /error_docs/internal_server_error.html
    ErrorDocument 405 /error_docs/method_not_allowed.html
    ErrorDocument 406 /error_docs/not_acceptable.html
    ErrorDocument 407 /error_docs/proxy_authentication_required.html
    ErrorDocument 412 /error_docs/precondition_failed.html
    ErrorDocument 414 /error_docs/request_uri_too_long.html
    ErrorDocument 415 /error_docs/unsupported_media_type.html
    ErrorDocument 501 /error_docs/not_implemented.html
    ErrorDocument 502 /error_docs/bad_gateway.html
    ErrorDocument 503 /error_docs/maintenance.html

    DirectoryIndex "index.html" "index.cgi" "index.pl" "index.php" "index.xhtml" "index.htm" "index.shtml"

    <Directory /var/www/vhosts/bio-test.ovh>
        AllowOverride AuthConfig FileInfo Indexes Limit Options=Indexes,SymLinksIfOwnerMatch,MultiViews,FollowSymLinks,ExecCGI,Includes,IncludesNOEXEC
    </Directory>

    <IfModule mod_headers.c>
    </IfModule>

    #extension letsencrypt begin
    Alias /.well-known/acme-challenge "/var/www/vhosts/default/htdocs/.well-known/acme-challenge"

    <Location /.well-known/acme-challenge/>
        # Require all granted
        Order Deny,Allow
        Allow from all
        Satisfy any
    </Location>

    <LocationMatch "^/.well-known/acme-challenge/(.*/|)\.">
        # Require all denied
        Order Allow,Deny
        Deny from all
    </LocationMatch>
    #extension letsencrypt end
</VirtualHost>
 

Asphyxia

Owner
Administrator
Apr 25, 2015
1,845
2
2,199
327
Hi, Does anyone know how to make the best redirects to www?

I have Apache2 and...

I have a few questions and tips to simplify your life greatly.

  1. Do you use Cloudflare or another load balancer solution? If not, do you need load balancing for control and efficiency.
  2. Why do you want to add www? You should be wanting to remove www because this is an outdated way of saying "We want to deal with the World Wide Web protocol." except when you are in a web browser - we already kind of know that. The purpose of "www." has fully vanished, there is ZERO reason to use "www." ever again in your life - get rid of this. If you want "blog." or "forum." okay but that serves actual purpose where "www." is like saying "welcome-to-var-www-html.yoursite.com" - literally just NO!!
  3. Do you have have mod_rewrite enabled in Apache, if not go ahead with
    Code:
    sudo a2enmod rewrite
    inside your terminal.
  4. Reference the above post by @NNE https://r4p3.net/threads/best-redirect-to-www.7491/#post-68433 but before applying this we could benefit from knowing if you are hosting any other subdomains.
Answer questions 1 and 2 before proceeding to tips 3 and 4.
 

Ondra3211

Contributor
May 5, 2018
65
37
111
I do not even know why I use www. Perhaps the best option would be to remove www. Unnecessary suffering.

@Asphyxia thank you for the beautiful answer and for explaining what is www

New question. How redirect www to non-www?
 
Last edited:

BIOS

Knowledge Seeker
VIP
Oct 10, 2015
447
848
208
fastest way with ssl

Code:
NameVirtualHost *:443
<VirtualHost *:443>
  ServerName example1.com
  SSLEngine on
  #...
</VirtualHost>
<VirtualHost *:443>
  ServerName example2.com
  SSLEngine on
  # same certificate config here as on example1, unless you're wanting to use TLS SNI
  # then, let's redirect the user to non-SSL
  Redirect permanent / http://example2.com/
</VirtualHost>
 
Last edited:

BIOS

Knowledge Seeker
VIP
Oct 10, 2015
447
848
208
It's all the same.
Apache config:
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTP_HOST} !=yourhost.fld
RewriteRule ^(.*)$ https://yourhost.fld/$1 [R=permanent,L]

your Apache config is .htaccess ;)
 

NNE

C++ priest
VIP
Jul 20, 2016
21
15
78
your Apache config is .htaccess ;)
LOL. These statements can be written in virtual host config.
Apache config:
 DocumentRoot /sites/somesite

        <Directory /sites/somesite>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all

        RewriteEngine on
        RewriteCond %{SERVER_PORT} !^443$ [OR]
        RewriteCond %{HTTP_HOST} !=somesite.fld
        RewriteRule ^(.*)$ https://somesite.fld/$1 [R=permanent,L]
        RewriteBase   /

        RewriteRule ^([^.]+)$ index.php

        </Directory>
 

Asphyxia

Owner
Administrator
Apr 25, 2015
1,845
2
2,199
327
LOL. These statements can be written in virtual host config.
Holy shit yeah :eek::p AAAAHHHHHHHHHHHHHHHHHH

.htaccess is just a nurse (or bandage) where the vhosts config is the hospital. No disrespect intended @BIOS but definitely favor vhosts configurations over a cute little Apache .htaccess lol
 

NNE

C++ priest
VIP
Jul 20, 2016
21
15
78
@BIOS Who said you were wrong? These statements can be placed everywhere. As I said .htaccess can be used only for testing, on development stage, on systems without access to VH config or when you know that nothing will be in danger.
 

Asphyxia

Owner
Administrator
Apr 25, 2015
1,845
2
2,199
327
im not wrong mates...
Look man, telling someone to use .htaccess is like telling someone they can brush their teeth without toothpaste.

The only reason you should ever use .htaccess is if you have no access to the Apache/httpd configs, even more specifically if you are on shared hosting. If you are not on a shitty shared hosting provider like GoDaddy which no one should ever be running on (NO SHARED HOSTING), especially because shared hosting sucks in 100 different ways including from a security viewpoint... then sure .htaccess ONLY if you are FORCED to use shared hosting I suppose!

If you have a VPS, dedicated server, or in any way can access the vhosts configuration files... USE THAT.

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.

This lovely blog post gives more details if you want to learn.

Ironically, R4P3.NET started out with hosting on a shared hosting web server. When our website exploded (got famous), our resource utilization peaked out past our limits. Whether this was due to attacks or actual user load, I still am not sure.

We moved from a shared web hosting provider to another one claiming "unlimited everything". That was definitely a lie, again we maxed out resources and they shut us down.

We moved to a VPS and this worked for a great period of time. Then our VPS hosting provider shutdown our server due to resource utilization.

This kept happening because everyone kept claiming "You only have virtual CPU and virtual RAM shared with everyone, your website is hogging all resources."

Eventually we were forced into going for a dedicated server, which we are with now and I plan on keeping us dedicated. Why did I go on about this? Have your own fucking configuration files, leave .htaccess for the GoDaddy kids that can only pay $1 for web hosting.
 
Last edited:
Top