Dxx
Dxx

Reputation: 934

Yii2 Advanced on Apache2: redirecting www to non-www

I have a Yii2 Advanced installation with sub-domains. My front-end domain is http://xxx.tld, however www.xxx.tld also works.

I want all www requests to be redirected to non-www. I tried using traditional methods but it doesn't appear to be working.

I have an A record for all my subdomains, and for @ and www

Here's my conf file for xxx.tld:

/apache2/sites-available/xxx.tld.conf

<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerName xxx.xxx
ServerAdmin [email protected]
DocumentRoot /var/www/xxx.xxx/frontend/web


    <Directory "/var/www/xxx.xxx/frontend/web/">
    # Redirect www to non-www
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L,NE]


        # If a directory or a file exists, use the request directly
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        # Otherwise forward the request to index.php
        RewriteRule . index.php
    AllowOverride All
        # ...other settings...
    </Directory>

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Upvotes: 2

Views: 690

Answers (1)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146460

Since you have direct access to main Apache configuration, there's no need to mess with mod_rewrite in the first place. Good old RedirectPermanent in appropriate containers should be enough:

#
# Secondary domains
#
<VirtualHost *:80>
    ServerName www.xxx.tld
    # Other alternatives domains:
    ServerAlias example.net www.example.net
    ServerAlias example.org www.example.org

    RedirectPermanent / http://xxx.tld/
</VirtualHost>


#
# Main site
#
<VirtualHost *:80>
    ServerName xxx.tld

    DocumentRoot /var/www/xxx.xxx/frontend/web
    # ...
</VirtualHost>

Upvotes: 3

Related Questions