LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Why isn't this site.conf working? (https://www.linuxquestions.org/questions/linux-server-73/why-isnt-this-site-conf-working-4175539836/)

wh33t 04-15-2015 05:56 PM

Why isn't this site.conf working?
 
Hey LQ,

I'm trying to set up a rewrite rule in site.conf. I just very simply want to make it so when visiting mydomain.com it forwards to www.mydomain.com.

Code:

<VirtualHost *:80>
        ServerName www.mydomain.net

        ServerAdmin contact@mydomain.net
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <directory />
                RewriteEngine On

                # www redirect
                RewriteCond %{HTTP_HOST} ^mydomain.net
                RewriteRule ^(.*)$ http://www.mydomain.net/$1 [R=301,L]

                # Disable Index Viewing
                Options -Indexes
        </directory>
</VirtualHost>

When I reload apache2 it doesn't error at all, yet still no forward occurs. Any suggestions?

astrogeek 04-15-2015 06:26 PM

The rewrite rule is inside the www.mydomain.net VirtualHost definition, so it is never seen and never used.

Set up VirtualHost for mydomain.net instead:

Code:

<VirtualHost *:80>
    ServerAlias mydomain.net
    RedirectMatch permanent ^/(.*) http://www.mydomain.net/$1
</VirtualHost>


wh33t 04-15-2015 08:11 PM

So, just to be clear I want my .conf to look like this:

Code:

<VirtualHost *:80>
        ServerName www.mydomain.net

        ServerAdmin contact@mydomain.net

        ServerAlias mydomain.net
        RedirectMatch permanent ^/(.*) http://www.mydomain.net/$1

        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <directory />
                RewriteEngine On

                # www redirect
                RewriteCond %{HTTP_HOST} ^mydomain.net
                RewriteRule ^(.*)$ http://www.mydomain.net/$1 [R=301,L]

                # Disable Index Viewing
                Options -Indexes
        </directory>
</VirtualHost>


astrogeek 04-15-2015 08:15 PM

No, actually more like this...

Code:

<VirtualHost *:80>
    ServerAlias mydomain.net
    RedirectMatch permanent ^/(.*) http://www.mydomain.net/$1
</VirtualHost>
<VirtualHost *:80>
        ServerName www.mydomain.net
        ServerAdmin contact@mydomain.net
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

You could also use ServerName in the mydomain.net block, but I always use ServerAlias for those, for reasons I don't really remember - but it doesn't matter.

You could put the ServerAlias alone inside the www.mydomain.net VirtualHost, without a redirect. But if you want clients to land on www and bookmark it, you should use the separate VirtualHost with redirect for that.

wh33t 04-15-2015 08:26 PM

Quote:

Originally Posted by astrogeek (Post 5348086)
No, actually more like this...

Code:

<VirtualHost *:80>
    ServerAlias mydomain.net
    RedirectMatch permanent ^/(.*) http://www.mydomain.net/$1
</VirtualHost>
<VirtualHost *:80>
        ServerName www.mydomain.net
        ServerAdmin contact@mydomain.net
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


Oh I think I get it now. Will that RedirectMatch forward any subdomain to www? Cause I don't want that. Just if no subdomain is present is what I want.

astrogeek 04-15-2015 08:28 PM

No, it will only redirect mydomain.net with no subdomains. Other subdomains will need their own VirtualHost blocks.

You might want to change the order of the VirtualHosts too, remember that the first VirtualHost will become the default for the server. Other than the first one being default order does not matter.

wh33t 04-15-2015 08:28 PM

Quote:

Originally Posted by astrogeek (Post 5348092)
No, it will only redirect mydomain.net with no subdomains. Other subdomains will need their own VirtualHost blocks.

Deadly bro! Thanks.

wh33t 04-15-2015 08:32 PM

Code:

<VirtualHost *:80>
    ServerAlias domain.net
    RedirectMatch permanent ^/(.*) http://www.domain.net/$1
</VirtualHost>
<VirtualHost *:80>
        ServerName www.domain.net

        ServerAdmin contact@domain.net
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

That's what I've got now in my .conf. Still doesn't work. I've done a service apache2 restart and reload. Still notta.

Do I gotta make sure some modrewrite or something is enabled?

wh33t 04-15-2015 08:39 PM

Perhaps I need to create another .conf file in sites-available? one for just domain.conf? and put the server alias stuff in that script instead?

astrogeek 04-15-2015 09:25 PM

Quote:

Originally Posted by wh33t (Post 5348096)
Perhaps I need to create another .conf file in sites-available? one for just domain.conf? and put the server alias stuff in that script instead?

That is largely a matter of personal choice and how your distro breaks up the config files.

I am not an Ubuntu user so can't help you much with what their defaults are.

I use Slackware which pretty much uses the Apache defaults and all my vhosts are normally in a single file. But as long as they all get included you can split them up any way that you like.

wh33t 04-15-2015 09:35 PM

Quote:

Originally Posted by astrogeek (Post 5348106)
That is largely a matter of personal choice and how your distro breaks up the config files.

I am not an Ubuntu user so can't help you much with what their defaults are.

I use Slackware which pretty much uses the Apache defaults and all my vhosts are normally in a single file. But as long as they all get included you can split them up any way that you like.

Thank you. I'll post this on the Ubuntu forums as well then.

bathory 04-16-2015 03:38 AM

Quote:

Originally Posted by wh33t (Post 5348096)
Perhaps I need to create another .conf file in sites-available? one for just domain.conf? and put the server alias stuff in that script instead?

After adding a .conf file in sites-available, you need to run a2ensite in order to enable the vhost in question (see step 5 here)

BTW you can also use the following for the domain.net vhost:
Code:

<VirtualHost *:80>
    ServerName domain.net
    Redirect permanent / http://www.domain.net/
</VirtualHost>

Regards


All times are GMT -5. The time now is 02:07 AM.