LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-15-2015, 05:56 PM   #1
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 921

Rep: Reputation: 61
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?
 
Old 04-15-2015, 06:26 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,258
Blog Entries: 24

Rep: Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193
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>

Last edited by astrogeek; 04-15-2015 at 06:29 PM.
 
1 members found this post helpful.
Old 04-15-2015, 08:11 PM   #3
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 921

Original Poster
Rep: Reputation: 61
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>
 
Old 04-15-2015, 08:15 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,258
Blog Entries: 24

Rep: Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193
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.

Last edited by astrogeek; 04-15-2015 at 08:26 PM.
 
1 members found this post helpful.
Old 04-15-2015, 08:26 PM   #5
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 921

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by astrogeek View Post
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.
 
Old 04-15-2015, 08:28 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,258
Blog Entries: 24

Rep: Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193
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.

Last edited by astrogeek; 04-15-2015 at 08:30 PM.
 
1 members found this post helpful.
Old 04-15-2015, 08:28 PM   #7
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 921

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by astrogeek View Post
No, it will only redirect mydomain.net with no subdomains. Other subdomains will need their own VirtualHost blocks.
Deadly bro! Thanks.
 
Old 04-15-2015, 08:32 PM   #8
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 921

Original Poster
Rep: Reputation: 61
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?
 
Old 04-15-2015, 08:39 PM   #9
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 921

Original Poster
Rep: Reputation: 61
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?
 
Old 04-15-2015, 09:25 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,258
Blog Entries: 24

Rep: Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193Reputation: 4193
Quote:
Originally Posted by wh33t View Post
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.
 
1 members found this post helpful.
Old 04-15-2015, 09:35 PM   #11
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 921

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by astrogeek View Post
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.
 
Old 04-16-2015, 03:38 AM   #12
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,159
Blog Entries: 1

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
Quote:
Originally Posted by wh33t View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bind DNS isn't working. Needing help with named.conf and zone file. MrMurdok Linux - Server 2 07-22-2014 05:44 AM
site down after messing with httpd.conf and suphp mikecoyne83 Linux - Server 4 11-14-2009 09:38 AM
less isn't working! mathman0 Linux - Software 2 02-20-2005 11:13 AM
Why isn't this working? Lgoat Programming 3 06-05-2004 08:20 AM
lilo.conf isn't doing what it should playmesumch00ns Linux - General 1 06-14-2001 03:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration