Forum Moderators: phranque
I had this big idea in my head involving multiple sites. All these sites are hosted on the same account and each has its own directory (including subdomains). There are some common behaviors I'd like between all sites.
For example let's say I have the following directories in my root directory of my account:
domain1.com
a.domain1.com
b.domain1.com
I don't have PEAR (well I'm fairly certain I don't) so I worked out a redirect system that worked well with my laptop. Essentially a 404 is generated for pretty much any page requested. The custom error page then looks to see if the file exists (i.e. 'account.i.php' would be checked if 'account.php' is requested). The script then outputs the banner, navigation and footer areas.... with the main area containing custom error messages or the page content (account.i.php exists in this case). If the page exists then the 404 is replaced with 200.
So I got this working on my laptop (w/i one domain) but now that I'm moving it to my hosting account I'm having some difficulties.
My problem is that my ErrorDocument line is not working. In this particular instance htaccess is in docroot and my test directory is one level down. On my laptop they were both the same. On my hosting account I'm getting "No input file specified."
So I looked online and see I'm not the first that encountered this problem and mod_rewrite was offered as a solution. So I tinkered with htaccess and now have:
IndexIgnore *
RewriteEngine On
RewriteRule /$ %{REQUEST_URI}index.php
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule %{REQUEST_URI}$ router.php?uri=%{SERVER_NAME}%{REQUEST_URI}
I guess IndexIgnore is kinda redundant now. So I fixed my index problem but I can't seem to figure out how to goto router.php. Essentially all page requests should goto that file (domain1.com/router.php). Ideally from any subdomain too, but I'm happy just to get it working for the main domain at the moment.
Is there a line I can add so that I can get a 404 for a missing PHP page (like my laptop) instead of "No input file specified."?
Thanks for your help
The fact is that if your return a 404 for every request, your site will never do well in search -- in fact, it might never even show up, and therefore, the whole "404-based dispatch" method is a non-starter.
The problem with the code above is apparently that you're trying to use a server variable as the RewriteRule pattern, and that is not going to work. I'd suggest reviewing the Apache mod_rewrite documentation and re-coding the whole mess as:
RewriteEngine on
#
RewriteRule ^(.+)/$ /$1/index.php [L]
#
RewriteCond %{DOCUMENT_ROOT}/$1 !-f
RewriteCond %{HTTP_HOST} ^(.+)\.?(:[0-9]+)?$ [OR]
RewriteCond %{SERVER_NAME} ^(.+)$
RewriteRule ^(.*)$ /router.php?uri=%1/$1 [L]
The "extra" stuff at the end of the HTTP_HOST pattern is intended to match (and discard) FQDN-format indicators and appended port numbers, so you don't have to handle those variants further. To be clear, the following are all valid values for HTTP_HOST:
example.com
example.com.
example.com:80
example.com.:80
www.example.com.
sub.example.com:80
sub.sub.example.com.:80
...etc.
Jim
[edited by: jdMorgan at 1:48 am (utc) on Oct. 31, 2009]
This could be done by adding a new RewriteCond as the first RewriteCond in the second rule above:
RewriteCond %{REQUEST_FILENAME} !\.php$ Jim
Re: all 404's not showing up on searches. I know that will be a problem. I remedied it on my laptop with:
if(file_exists($page)) {
header("HTTP/1.x 200 OK\r\n");
$require = true;
}
That snippet is at the top of the template script. Based on how things are going so far, I have no idea if that will work under my hosting account's setup. The whole concept behind this redirect madness is to let a user decide which template they would like to use for their site layout. And to make modifying templates easy. (i.e. one file in a 600 page site instead of 600)
So basically page requests go to router.php.... (based on the server setup, I think only .php page requests as .html will generate a 404 and Error Document will work). That script routes them to the proper template script. The template script "draws" the page except for the main area. That is included if $require === true.
I'll take a better look at my hosting account. I have a bunch more to do on the site before it's ready. I just thought this might be "quick and easy" and I could continue on with my other stuff. Thanks for your help.
Is there any way to see what the rewrites are doing? Knowing that, I'll be able to tinker with it and figure it out.