I am developing a PHP-based CMS which runs mainly from a shared codebase, running multiple domains on a single server.
I have purposely left many static files which are put into public directories for each domain, this gives me the option to override default settings, or calls to templates for any client.
I find I am increasingly dependent upon using mod_rewrite.
I currently keep these rules in .htaccess files in each domain’s top public directory.
e.g.
/public_html/domain1.com/.htaccess
/public_html/domain1.com/diary.php
/public_html/domain1.com/about.php
The links from about.php might contain links such as
/diary/kids-day-out-jan2010
My .haccess file contains rewrite rules which rewrites that url to /diary.php?event=kids-day-out-jan2010
So to my question.
Which would be the most manageable and possibly least error-prone way to store and apply these mod_rewrite rules?
Is this something you would store in a per domain declaration in the conf file or not?
Is there another system I could be looking at?
Is there a further hierarchy of apache directives that I could look at if for example I wanted to apply the /diary.php?event=kids-day-out-jan2010 rewrite to every single domain ?
mod_rewrite’s Technical Details document explains [quote=http://httpd.apache.org/docs/2.2/rewrite/rewrite_tech.html]“when Apache processes a HTTP request it does this in phases. A hook for each of these phases is provided by the Apache API. Mod_rewrite uses two of these hooks: the URL-to-filename translation hook which is used after the HTTP request has been read but before any authorization starts and the Fixup hook which is triggered after the authorization phases and after the per-directory config files (.htaccess) have been read, but before the content handler is activated.”[quote]Thus, you can’t write a mod_rewrite statement to apply across domains.
The next part of your question asks where the best location is for your mod_rewrite statements. If you have access to the httpd.conf (httpd-vhosts.conf), that is clearly the best place for a domain’s mod_rewrite as it only needs to be read ONCE (NOT for each file request). Your code will then apply across the domain as if it’s in the DocumentRoot.
The least desirable place is in .htaccess files. Of course, most webmasters do not have access to httpd.conf (or httpd-vhosts.conf) so this is the default location for many. If you don’t have a test server, it’s also the place where you can test your mod_rewrite code before committing it to the conf files. Because the .htaccess file (every one in the path to the file requested) must be read with EVERY request, this is CPU intensive and will slow the server (you should see some of the LONG lists of mod_rewrite statements some people show here - what a waste of CPU cycles! NOT for shared server, certainly!).
Main takeaways:
No I cannot set a mod_rewrite rule which applies to multiple domains.
I do have a dedicated server and have access to the httpd.conf file but it still means that in each domain declaration I need to hard-code the rewrite rules.
I have checked through some of the relevant docs and cannot see any obvious gotchas, I should just be able to take the rules that work in my current .htaccess file and paste them into the domain declaration - and they should just work?
Do the apache directives need to be called in any particular order?
( a few weeks ago I got a solution on here using mod_alias that was a real revelation to me, which is partly which lead me to start this thread really )
I have another question too, if I am not being too cheeky.
If I created an illegal rule in my .htaccess file, the server just threw a 500 error straightaway, if I make a similar mistake in a httpd.conf file do I still look in my standard error file to find out what caused it?
Because I need to restart apache every time I add a domain/rule, I am wondering who writes the error out and where?
“The Definitive Guide to Apache mod_rewrite” I came across a link to that when I was searching for httpd.conf rule examples, looks as if I’ll need to shell out for that.
Perhaps. I was a little disappointed as I got that after I wrote my signature’s tutorial (to save me from repetitively answering the same questions over and over …). It’s emphasis on the httpd.conf forms (I believe it’s been updated from my Apache 1.x version) left me to my own devices for the “normal” .htaccess form of mod_rewrite statements.
It also left the RewriteMap section a little “light.” That can be a wonderful tool for those with access to the server’s configuration file.
IMHO, you’ll get as much from apache.org as you will from Bowan. Try that first, THEN get Bowen’s book if you really need it.