Simplifying my Apache site configuration

Hi Everyone,

I run a website off of a Ubuntu server (10.04 LTS, I think) with Apache, MySQL, and PHP. It serves both HTTP and HTTPS content. Currently, I have two main tags – one for HTTP and one for HTTPS -- and I'm copying/pasting the same rewrite rules into both of them.

Is there a better way of doing this so I don't have to put the same rewrite rules in twice? Could I create a tag that would apply to both HTTP and HTTPS and put the rewrite rules in there?

Thanks for your help!

Here's a modified version of my Apache site configuration file (the real one has more rewrite rules):

 <virtualhost *:80="">ServerAdmin webmaster@localhost

    DocumentRoot /var/www
     <directory>Options FollowSymLinks
        AllowOverride None</directory> 
     <directory var="" www="">Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all

AddType video/webm .webm

Redirect 301 /pricing-and-signup/ /plans-and-pricing/

RewriteEngine on
RewriteRule ^tryv3/?$ /index.php [L]
RewriteRule ^TRYV3/?$ /index.php [L]

RewriteRule ^404/?$ /Error.php?Error=404 [L]
ErrorDocument 404 /404/</directory> 

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
     <directory "="" usr="" lib="" cgi-bin"="">AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all</directory> 

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <directory "="" usr="" share="" doc="">Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128</directory></virtualhost> 

 <virtualhost xxx.xxx.xxx.xxx:443="">ServerAdmin webmaster@localhost
    ServerName [REDACTED]

    SSLEngine On
    SSLCertificateFile [REDACTED]
    SSLCertificateKeyFile [REDACTED]
    SSLCertificateChainFile [REDACTED]

    DocumentRoot /var/www
     <directory>Options FollowSymLinks
        AllowOverride None</directory> 
     <directory var="" www="">Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all

AddType video/webm .webm

Redirect 301 /pricing-and-signup/ /plans-and-pricing/

RewriteEngine on
RewriteRule ^tryv3/?$ /index.php [L]
RewriteRule ^TRYV3/?$ /index.php [L]

RewriteRule ^404/?$ /Error.php?Error=404 [L]
ErrorDocument 404 /404/</directory> 

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
     <directory "="" usr="" lib="" cgi-bin"="">AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all</directory> 

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <directory "="" usr="" share="" doc="">Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128</directory></virtualhost> 

The rewrite rules that are repeated:

AddType video/webm .webm

Redirect 301 /pricing-and-signup/ /plans-and-pricing/

RewriteEngine on
RewriteRule ^tryv3/?$ /index.php [L]
RewriteRule ^TRYV3/?$ /index.php [L]

RewriteRule ^404/?$ /Error.php?Error=404 [L]
ErrorDocument 404 /404/

8 Replies

Hi jzimmerlin,

As per the Apache 2.2 documentation, http://httpd.apache.org/docs/2.2/mod/co … irtualhost">http://httpd.apache.org/docs/2.2/mod/core.html#virtualhost simply do:

 <virtualhost 1.2.3.4:80="" 1.2.3.4:443="">...</virtualhost> 

Regards,

Thanks, I'll give that a shot!

@AlexC:

Hi jzimmerlin,

As per the Apache 2.2 documentation, http://httpd.apache.org/docs/2.2/mod/co … irtualhost">http://httpd.apache.org/docs/2.2/mod/core.html#virtualhost simply do:

 <virtualhost 1.2.3.4:80="" 1.2.3.4:443="">...</virtualhost> 

Regards,
OK, so I finally got around to trying this. Unless I misunderstood what you meant, this didn't work.

 <virtualhost *:80="" 72.14.190.162:443="">ServerAdmin webmaster@localhost
    ServerName www.rundowncreator.com

    SSLEngine On
    SSLCertificateFile /etc/apache2/ssl/STAR_rundowncreator_com.crt
    SSLCertificateKeyFile /etc/apache2/ssl/rundowncreator.com.key
    SSLCertificateChainFile /etc/apache2/ssl/PositiveSSL.ca-bundle

    DocumentRoot /var/www
     <directory>Options FollowSymLinks
        AllowOverride None

(REWRITE RULES WOULD GO HERE)</directory> 
     <directory var="" www="">Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all</directory> 

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
     <directory "="" usr="" lib="" cgi-bin"="">AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all</directory> 

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <directory "="" usr="" share="" doc="">Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128</directory></virtualhost> 

…will force SSL on port 80 and 443, which is not what I want. And…

 <virtualhost *:80="" 72.14.190.162:443="">ServerAdmin webmaster@localhost

    DocumentRoot /var/www
     <directory>Options FollowSymLinks
        AllowOverride None

(REWRITE RULES WOULD GO HERE)</directory> 
     <directory var="" www="">Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all</directory> 

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
     <directory "="" usr="" lib="" cgi-bin"="">AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all</directory> 

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <directory "="" usr="" share="" doc="">Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128</directory></virtualhost> 

 <virtualhost 72.14.190.162:443="">ServerAdmin webmaster@localhost
        ServerName www.rundowncreator.com

        SSLEngine On
        SSLCertificateFile /etc/apache2/ssl/STAR_rundowncreator_com.crt
        SSLCertificateKeyFile /etc/apache2/ssl/rundowncreator.com.key
        SSLCertificateChainFile /etc/apache2/ssl/PositiveSSL.ca-bundle</virtualhost> 

…gives me:

[Fri May 04 10:00:31 2012] [warn] VirtualHost 72.14.190.162:443 overlaps with VirtualHost 72.14.190.162:443, the first has precedence, perhaps you need a NameVirtualHost directive

Any ideas?

You may have more success putting the common configuration in a third file, and including that file from each vhost. The warning message is telling you to add a NameVirtualHost directive. If that doesn't do it, I wouldn't be sure how to mix non-secured and secured vhost definitions either.

How would you include the file?

http://httpd.apache.org/docs/2.2/mod/core.html#include should do it.

I'll give that a shot, thanks!

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct