.htaccess url-rewrite if file not exists - .htaccess

I must do a little trick for a site!
The idea is:
if a file for a required url exists then I go to that url, doing nothing more;
if a file for a required url not exists, I must go to a file.php and than do something, but NOT changing the url!
example:
www.mysite.com/page1.htm -> exists -> go to file page1.htm
www.mysite.com/page2.htm -> NOT exists -> go to file default.php but with url "www.mysite.com/page2.htm"
It's possible to do this all by .htaccess?

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /default.php [L]

It is not mentioned here but FallbackResource is the new recommended way of handling not-found (404) URLs. Example:
FallbackResource /not-404.php
From Apache manual:
Use this to set a handler for any URL that doesn't map to anything in your filesystem, and would otherwise return HTTP 404 (Not Found).

Implement a 404 error rule. Doesn't require mod_rewrite:
ErrorDocument 404 /default.php

Related

.htaccess file (save request_filename) as querystring

I am using my htaccess file to forward users to a specific page when they request a notfound page. So far i have this:
ErrorDocument 404 /notfound.asp
I also want to pass the bad filename that they tried to use to this page using a querystring value. So I want something like
ErrorDocument 404 /notfound.asp?badfilename=HERE
I have been trying to play wround with URL rewrite and using %{Request_Filename} but I am new to working with .htaccess code. Is it possible to pass the bad filename to the notfound.asp with the bad file name as a querystring value? (I have code in my notfound.asp page that will retrieve the bad file name querystring value for tracking purposes.)
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /notfound.asp?badfilename=$1 [L,QSA]
Reference: Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details

404 redirect using .htaccess

I want to set up a rule in my .htaccess file so that any url that is enetered, that results in a 404 because there is no such file, automatically re-directs to the home page of the site:
index.php
my .htaccess file looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^queenslandbeerweek.com.au$ [NC]
RewriteRule ^(.*)$ http://queenslandbeerweek.com.au/$1 [L,R=301]
ErrorDocument 404 /index.php
This causes the index.php file to show but is broken and leaves the eroneous URL in the address bar.
I have read in the answer to another post that it has something to do with passing the erroneous URL as a parameter, causing the page to not load properly, because the page calls data from a database and it is passing the bad URL as a parameter of index.php but there was no hint as to what the solution is.
What I would like to happen, is if an incorrect URL is typed into the address bar, or if a link is followed, to a file that does not exist, the completely forget about this file, drop everything, and go to the home page index.php.
index.php calls data from a database
Is this possible using a .htaccess file?
I have exactly the same problem with another of my sites.
Any help would be greatly appreciated.
Cheers, Al.
I dont think you can directly redirect an error document but you can catch nonexistent files and folders
!-f means not a file !-d means not a directory, $1 is whatever is in (.*) (the path in the url)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?errorurl=$1 [R=301,L]
ErrorDocument 404 /404.php
You can place the 404 error template anywhere you want. For example you could place all error messages in a folder called errormessages
ErrorDocument 404 /errormessages/404.php

.htaccess - 404 error

I want .htaccess to return error 404 for any url that does not meet below url pattern criteria. How can I do this in .htaccess?
Using this code in php script generates above url
$adscatcanonurl = "$script_url" . "/" . "{$vbasedir}$xcityid-$xcitynamealt/posts/$xcatid-$catname_inurl/". ("$xsubcatid"?"$xsubcatid-". RemoveBadURLChars($xsubcatname) :"");
Sample link
http://www.domain.com/17-Netherlands/posts/8-Real-Estate/
Let's say script generates a link like this
http://www.domain.com/17-Netherlands/posts/8/
As it's not matching with above url pattern, such urls should return 404 page not response.
You don't need to force .htaccess to return a 404, it will simply do so on its own if there is no match. So you can use a rule like:
RewriteEngine On
# Don't match real files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\d+)-([a-zA-Z-]+)/posts/(\d+)-([a-zA-Z-]+)$ your_php_script.php?xcityid=$1&xcitynamealt=$2&xcatid=$3&xcatname_inurl=$4 [L,QSA]
Any request that isn't for an actual file (css, js, etc) and doesn't match the above rule won't match any rule, and should therefore return a 404.
Note that in the rule, I mapped the 4 components to what would be read in your PHP as $_GET['xcityid'], etc.
Why not use: ErrorDocument 404 /404page.html
http://www.yourhtmlsource.com/sitemanagement/custom404error.html

RewriteCond to skip actual directories seems to be ignored

My problem is that The RewriteRule is still matching when I visit a physical directory, e.g. http://a-domain.com/foo/ where foo is a normal directory in the web root.
the .htaccess file has:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .? index.php [L]
It works fine on my dev server but not on a live WHM/CPanel server. I'm a bit lost.
I've tested this on my cpanel website and you're right and I found out where's the problem. when you try to access a folder in your website that doesn't have default file (index) , it tries to access the file that is responsible for 403 HTTP code, and because that doesn't exist, it rewrites URL to index.php.
All you have to do is add this to above of your .htaccess file:
ErrorDocument 403 /index.php?type=err&code=403
I tried this on a VM which mirrors my own shared service and it works fine for me. Have you got another .htaccess in the foo directory? If so, then the rewrite engine will ignore your one in DOCROOT and use this instead?
The /foo/ dir has basic authentication so it was failing when %{REQUEST_FILENAME} contained a non-existent file /home/the_user/public_html/401.shtml. I didn't think this was an issue because the basic authentication worked, and when I cancelled the auth prompt I was being served a standard 401 file. Usually when those ErrorDocument files are missing, Apache says Additionally, an error of type 404 was encountered while trying to use an ErrorDocument to handle the request.

How can I get non existant files mapped correctly in .htaccess?

Duplicate:
How to rewrite non existant files to
‘default’ files?
(.htaccess)
How would I "rewrite" to a location if a file doesn't exist? I don't want to use a 404 redirect, but an actual rewrite.
So for example, let's say it is a directory with images. If the image isn't found, then it rewrites to a default image?
I.e.,
images/1.jpg
images/2.jpg
images/default.jpg
if someone tried to access "website.com/images/3.jpg",
since that doesn't exist, I want it to go to:
"website.com/images/default.jpg"
This was a previous "posted" solution, but didn't quite work:
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule /images/.* /images/error.jpg [L]
It still doesn't "get" the right image (just goes as a regular 404 request).
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^images/.* /images/error.jpg [L]
Obviously this only redirects if missing file is under /images/... but you can easily modify it for your own needs
Well, your previous posted solution is on the right track, but there's some slight craziness with it. Try this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule images/.* /images/default.jpg [L]
You should better send a 404 status code if the file really doesn’t exist rather than just a substitute with a status code other than 404. Otherwise the URL will be handled as valid.
So in your case I recommend you to set the ErrorDocument of the images directory to your default image:
<Directory "/path/to/your/images/">
ErrorDocument 404 /images/default.jpg
</Directory>
But note that the <Directory> block is only available in the server or virtual host configuration context and not the per-directory context (thus .htaccess).
If you cannot use the above, you could use a custom script as your custom error document to check what URL has been requested (see Request_URI environment variable) and send the default image if necessary. The ErrorDocument directive then should look like this:
ErrorDocument 404 /your-error-404.script
re-write your 404 document for your images folder:
(In your .htaccess file in your images folder)
ErrorDocument 404 default.jpg

Resources