.htaccess Check for file in two locations, otherwise throw 404 - .htaccess

The way my site is currently structured, the actual site is in its own separate folder. Here's what I mean:
/projects
/files
/pictures
/tools
/school
/~webroot
.htaccess
This makes the file-system much easier to manage and navigate. An easy way to utilize this, without having everyone navigate to http://domain.com/~webroot/, and still allow them to access files and such like http://domain.com/projects/, is to use the htaccess I wrote below to check for files in both the real root, and ~webroot directories.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /~webroot/$1 [NC,QSA]
RewriteRule ^/?$ /~webroot/index.php [L,QSA]
However, if the file doesn't exist anywhere (root or ~webroot), an HTTP 500 error is thrown instead of an HTTP 404. In order to display my 404 error page, I have to instead use these lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/~webroot/$0 !-F
RewriteRule ^(.*)$ /404.shtml [B,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^/~webroot
RewriteRule ^(.*)$ /~webroot/$1 [NC,QSA]
RewriteRule ^/?$ /~webroot/index.php [L,QSA]
This is all quite messy, and it's only a trick and doesn't actually throw an HTTP 404, which keeps 404s from being documented using my statistics application. Is there a way to: Check if file exists in root, if not check if file exists in ~webroot, if not throw real HTTP 404 error?
I do also have all my ErrorDocument's defined properly.

I know this was a while ago, but wanted to chime in. For a rewrite rule to hit a 404, I always use this:
RewriteRule ^(.*)$ - [R=404,L]
I'm not sure if it's correct, but it works for me. I'm pretty sure it should always be with "L" to prevent further rewriting.
BTW, what is the "B" flag for? I only know the ones listed on the apache mod_rewrite page, so I'm lost on it's meaning.
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
EDIT: ah, my problem is I look at older version docs. Thank you, Swivelgames. I probably would have never even known I was looking at the older docs if you hadn't pointed that out (need to update my bookmarks).
http://httpd.apache.org/docs/2.3/rewrite/flags.html#flag_b

If you can make your 404 page a PHP file, you can add this before any output to get real 404's:
<?php header("HTTP/1.1 404 Not Found"); ?>
This will cause PHP to trigger a 'real' 404 (from the browsers point of view) which gets passed back to the browser. If your statistics package is internal to your server this approach may or may not work, I'm not sure.
The only other solution I can think of is to redirect to a non-existent file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !/non-existent-404-generator
RewriteCond %{DOCUMENT_ROOT}/~webroot/$0 !-F
RewriteRule ^(.*)$ /non-existent-404-generator [B,L]

Related

.htaccess - alias domain root to subfolder

I've got an install of concrete5 living in my site root - the problem is that the C5 file structure is pretty varied, and it's getting mixed in with my subdirs and subdomains. I can't tell what's my code and what's C5, I hate it.
My solution is to move everything to a /_concrete/ folder - but since my domain has to point to the root, I can't use the files here.
Enter .htaccess: I need to write a script to redirect any instance of www.domain.com (ignore subdomains) to www.domain.com/_concrete/ - but not a transparent 301, just an alias. To clarify: www.domain.com/page/ and www.domain.com/_concrete/page/ should display the same thing. www.domain.com/page/ should NOT change it's URL to www.domain.com/_concrete/page/.
I've adapted this from a similar script I use elsewhere:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /_concrete/$1 [L,QSA]
But this only causes a server error. I hope I'm close - can anyone point me in the right direction?
You're telling the server to check if the value is not a filename, but you're not having it ignore the _concrete directory itself (which would also match your script and create a loop).
This might get you closer to what you're trying to do.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !^_concrete
RewriteRule ^(.*)$ _concrete/$1 [L,QSA]
Additionally you might want to have it ignore directories that exist as well. At which point _concrete would no longer be matched because it exists.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ _concrete/$1 [L,QSA]

.htaccess. Redirect requests to ~username folder

Situation:
I'm moving a website from a production environment to a test environment.
The test environment url is similar to http://192.168.1.100/~username/
There are thousands of files which use the following within the html
<img src='/images/image.jpg' />
Since the request is going to root http://192.168.1.100/ the files are 404.
Rather than finding and replacing all of html I'd assume that there is an easy way to fix it with mod_rewrite via .htaccess.
I've tried using the following
RewriteCond %{REQUEST_URI} !^/~username/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /~username/$1
But did not work as expected.
Thanks in advance.
UPDATE
The development environment resides within cpanel/whm. So when the username is removed from the requested url, it now belongs to the root users. So, my question now: How do I update the .htaccess file for the root user to mod_rewrite back to the ~username?
If you remove
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
it appears to work as expected, because any request to the right url will not be rewritten.
you might want to add [L] as a flag to signify it's the last rewrite rule, like so:
RewriteCond %{REQUEST_URI} !^/~username/
RewriteRule ^(.*)$ /~username/$1 [L]

Possible .htaccess errors, 1and1.com not rewriting

The following .htaccess file works perfectly on my local server.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.cssc
RewriteRule . style.php [L]
RewriteRule ^admin\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . admin.php [L]
I am doing some work for a client and he is using 1and1.com. I do not know anything about his account or what package he has.
All files are rewritten to admin.php (unless they actually exist). The problem is, I am getting a 404.
I know it's reading the .htaccess file because:
I can put garbage in the file and get a 500 error.
If I do a general rewrite (all pages go to admin.php), it works.
Also, it seems that 1and1 does it's own rewriting. If I go to: http://somewhere/afile, it will include afile.js even though I am not requesting the .js.It is super strange.
Does anyone have any experience with this? Or any insight?
To add some information..
1and1 seem to have some sort of default rewriting already in place that can interfere with your own rules - for example:
RewriteRule ^product$ /product.php [L]
It gave a 404 for this, and also for any other rewrite rule that matched a pre-existing .php file in my root folder.
2 things that 'fixed' it:
Change rewrite text to no longer match filename:
RewriteRule ^productz$ /product.php [L]
Or, change filename:
RewriteRule ^product$ /product_.php [L]
Both work - both very annoying.
The answer was simply "Do not nest .htaccess files". I had one in a sub folder.

What does this .htaccess code mean?

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
I think it means if the url doesn't match a file or directory on the server go to /index.php?
Can anybody confirm?
Yes is the short answer. But usually ./index.php has an [L,QSA] suffix/qualifier, so the query part of a URI request is also passed on to it.
What this is doing:
RewriteCond %{REQUEST_FILENAME} !-d
If it can't find a directory with the name
RewriteCond %{REQUEST_FILENAME} !-f
If it can't find a file with the name
RewriteRule .* ./index.php
Load the index.php next to the .htaccess file.
To answer Anthony's question regarding what would happen if it where removed1 2,
It would try and access the directory or file. If it fails, instead of defaulting to the index.php it would simply give the default web servers 404 error.
Right now, it allows the application to either show the default page, or handle the request through it in some fashion.

Using mod_rewrite only if a 404 would have occured

I am in the process of converting a static website into one using a cms.
I have the cms installed in a sub directory of the public directory. To avoid ending up with ugly domain names (http://example.com/cms/) is there an easy way using mod_rewrite to rewrite http://example.com/… to http://example.com/cms/… while ensuring that if the request wouldn't have ended in a 404, there is no redirect.
An example:
/
/cms/index.html
/cms/file.dat
/file.dat
If the user requests /index.html, they should get redirected to /cms/index.html, but if they request /file.dat, they shouldn't get redirected to /cms/file.dat because the file existed at the requested place
EDIT
Thanks for the answers.
You could use the RewriteCond Directive to check whether there is an existing file that correspond to the requested URL, and only rewrite to your CMS if there is none.
Here is a simple example :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
If there is no existing file that correspond to the requested URL, then that request is rewritten to index.php
You might also want to check for symbolic links and / or directories, btw...
For instance, here is a possibility that can be used when setting up a Zend Framework project :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
(Even though it links to ZF, it should be OK for quite many projects)
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^cms/ cms%{REQUEST_URI} [L]

Resources