[Intermediate] Use mod_rewrite for performance


(Avi Miller) #1

While Matrix can (and does) support lots and lots of sites in a single install, it can be better for performance to only configure a single URL for each site in Matrix and use Apache's mod_rewrite rules to handle the rest.


For example, you may have domain.com and www.domain.com. Currently, you would configure both URLs on the Site asset in Matrix, but this results in at least two URLs for every asset. By using mod_rewrite, we can halve the number of database records in at least two tables (sq_ast_lookup and sq_ast_lookup_value for those of you playing along at home).



So, in the above example, let's rewrite all domain.com URLs to their equivalent www.domain.com URL:



Example code:


    
    ServerName www.domain.com
    ServerAlias domain.com
    ServerAdmin webmaster@domain.com
    
    # Rewrite all domain.com to www.domain.com
    
    # The RewriteCond says "Anything that doesn't match www.domain.com"
    RewriteCond %{HTTP_HOST}   !^www.domain.com$ [NC]
    
    # The RewriteRule says "Rewrite to www.domain.com with the original path in place"
    RewriteRule ^/(.*)$		http://www.domain.com/$1 [L,R]
    
    # Insert Matrix aliases after the rewrite rules
    Alias "/__fudge" /path/to/matrix/fudge
    Alias "/__data"  /path/to/matrix/data/public
    Alias "/__lib"	/path/to/matrix/core/lib
    Alias "/" /path/to/matrix/core/web/index.php/
    
    


Notice that we do have [b]domain.com[/b] configured as a ServerAlias in this VirtualHost block. This is because we want Apache to route requests for domain.com to this block so that our rewrite code is processed.

You can do the same for short names to full names. For example, you could have [b]intranet[/b] and [b]intranet.domain.com[/b]. In this case, we'll use the FQDN (intranet.domain.com) in Matrix, because the cookie security prefers it.

(Justin Cormack) #2

Its not just a performance tip - you want all your SEO on one domain too…


(Nic Hubbard) #3

We just set this up, and it is working nicely.


Would it be safe to remove puc.edu from our system root URLs and only leave www.puc.edu?


(Rhulse) #4

[quote]We just set this up, and it is working nicely.


Would it be safe to remove puc.edu from our system root URLs and only leave www.puc.edu?[/quote]



We do a redirect, rather than a rewrite, and have removed the non www url from Matrix. (I prefer to have one canonical URL for each page). You might even see a slight performance increase as Matrix no longer has to manage cache entries for both sets of URLs.


(Avi Miller) #5

Not just cache entries: By removing the URL, you are halving the number of sq_ast_lookup entries and at least halving (if not more) the entries in sq_ast_lookup_value, depending on how many designs and paint layouts you have.

(Anton Babushkin) #6

Sounds like something worth while considering for our infastructure. Things are incredibly slow at the moment…


(Avi Miller) #7

There are other considerations for your particular site, but yes -- this would be part of the solution, I suspect.

(Nic Hubbard) #8

Would this have any affect on Matrix remaps? We had a bunch of parent domains (I think that is what they are called!) such as enroll.puc.edu and apply.puc.edu which were set in the remap manager to remap to their appropriate pages. Now, it seems that after adding the Apache remaps, these only go to our PUC homepage.


Would these Apache remaps have had an effect?


(Avi Miller) #9

Potentially, yes -- if the old URLs are being re-written by mod_rewrite before Matrix sees them. You'd need to evaluate whether or not Matrix is seeing the correct old URL. Otherwise it can't do the remapping properly.

Also keep in mind that Matrix does not need to have old URLs applied to actual assets in order to remap them. It just needs to see the requests.

(L5x12w19) #10

[quote]While Matrix can (and does) support lots and lots of sites in a single install, it can be better for performance to only configure a single URL for each site in Matrix and use Apache's mod_rewrite rules to handle the rest.


For example, you may have domain.com and www.domain.com. Currently, you would configure both URLs on the Site asset in Matrix, but this results in at least two URLs for every asset. By using mod_rewrite, we can halve the number of database records in at least two tables (sq_ast_lookup and sq_ast_lookup_value for those of you playing along at home).



So, in the above example, let's rewrite all domain.com URLs to their equivalent www.domain.com URL:



Example code:


    
    ServerName www.domain.com
    ServerAlias domain.com
    ServerAdmin webmaster@domain.com
    
    # Rewrite all domain.com to www.domain.com
    
    # The RewriteCond says "Anything that doesn't match www.domain.com"
    RewriteCond %{HTTP_HOST}   !^www.domain.com$ [NC]
    
    # The RewriteRule says "Rewrite to www.domain.com with the original path in place"
    RewriteRule ^/(.*)$		http://www.domain.com/$1 [L,R]
    
    # Insert Matrix aliases after the rewrite rules
    Alias "/__fudge" /path/to/matrix/fudge
    Alias "/__data"  /path/to/matrix/data/public
    Alias "/__lib"	/path/to/matrix/core/lib
    Alias "/" /path/to/matrix/core/web/index.php/
    
    


Notice that we do have [b]domain.com[/b] configured as a ServerAlias in this VirtualHost block. This is because we want Apache to route requests for domain.com to this block so that our rewrite code is processed.

You can do the same for short names to full names. For example, you could have [b]intranet[/b] and [b]intranet.domain.com[/b]. In this case, we'll use the FQDN (intranet.domain.com) in Matrix, because the cookie security prefers it.[/quote]


where will i insert these codes?

(Mark Brydon) #11

The VirtualHost entry is inserted into the Apache virtual hosts file.

Sometimes this is in the httpd.conf or apache2.conf file, but usually this is separated out into a separate file in the Apache config directory (eg; /etc/apache2).



Apache may be located under a different path depending on your configuration.

In any case it is possible to determine the location of the virtual hosts file by referring to the main configuration file, which may have an "Include" statement for this file.



Edit: Removed extraneous line breaks


(Nic Hubbard) #12

Just an update, we never got Avi's RewriteCond to work, but the following does work for us:

    RewriteCond %{HTTP_HOST} ^puc.edu$ [NC]

(Tbaatar) #13

Hi Avi,


This works great.



If I have 2 domain, is it just the case of adding more Server Name/Alias on top the /etc/apache2/httpd.conf?



like this:


    
    ServerName www.domain2.com
    ServerAlias domain2.com
    RewriteCond %{HTTP_HOST}   !^www.domain2.com$ [NC]
    RewriteRule ^/(.*)$        http://www.domain2.com/$1 [L,R]