I was given a set or re-write rules for a new website. I've never used them before and I'm trying to understand what we are doing here
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.myco\.com
RewriteRule (.*) http://www.myco.com/$1 [R=301,L]
RewriteCond $1 !^(CCSFG|images|fonts|themes|robots\.txt|index\.php) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
If I understand this, the first ruleset
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.myco\.com
RewriteRule (.*) http://www.myco.com/$1 [R=301,L]
says:
If HTTP_HOST is anything AND
if HTTP_HOST doesn't start with www.myco.com
then
rewrite any page request to start with http://www.myco.com/, return a 301 redirect and stop processing more rules
The next ruleset
RewriteCond $1 !^(CCSFG|images|fonts|ee-system|themes|robots\.txt|index\.php) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
says: If the request doesn't start with CCSFG,image or fonts or, etc (non-case sensitive) then rewrite the whole request to prepend /index.php/ and stop processing more rules
Is my interpretation correct?
If so, what would be the purpose of the second ruleset? Why would I want to prepend /index.php/ to these entries?
Yup, pretty much exactly.
The first
RewriteCond %{HTTP_HOST} .
exists so as to not redirect loop someone who isn't sending a host header - it's verifying that the host header variable contains at least one character.The prepending behavior exists to force page requests to be handled by
index.php
, likely for pretty URLs - the exceptions are for locations that need to be served by a physically present file.So, for
/images/image2.jpg
, it's letting Apache serve the file at that location, but for/contentpage/1/2/3
it's rewriting to/index.php/contentpage/1/2/3
, letting the PHP app do its thing.Actually, it's technically rewriting to
//index.php/contentpage/1/2/3
; it's clear that it has a changed base (from being in an htaccess file), and it's not taking that into account for its destination.