.htaccess - 404 error - .htaccess

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

Related

Dynamic 404 Page.. kinda

Alright, so this is what I am trying to achieve.
I want to be able to send URL data to a php file if the server returns 404.
for example:
example.com/stackoverflow
would bring up a dynamic 404 page
(404.php?id=stackoverflow)
but I am not using it for 404 in my case, I want to send the data after the domain.com/
So that I can pull from my database and display content accordingly.
I know this can be done with a small rewrite in the .htaccess, regex is just confusing for me.
I do NOT want a redirect.
http://example.com/datahere
should show the data of
404.php?id=datahere
You can use ErrorDocument directive to redirect 404 uris to /404.php something like the following :
ErrorDocument 404 /404.php?id=%{REQUEST_URI}
This will rewrite /datahere to /404.php?id=datahere ( /datahere will show you the contents of /404.php?id ) .
Note that the above directive doesn't work on apache versions bellow 2.4 as the mod-rewrite variable %{REQUEST_URI} is treated as plain text not as a variable on lower versions.
On apache 2.2 you could use mod-rewrite to rewrite non-existent requests to 404 . Use the following :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /404.php?id=$1 [L]

.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

htaccess redirect if URL is bad - how to do this?

I want to redirect the links which are bad, getting 404 errors.
Let's say I have the following url:
http://www.example.com/foobar
If the above url is bad, I want it to redirect it with .html suffix
http://www.example.com/foobar.html
So basically I want to make sure URL is bad and it doesn't end with .html, then only apply the rule, which is same URL with .html suffix.
UPDATE: It's not just foobar.html, there are thousands of pages dynamic, so simple use of /foobar.html to redirect will not work.
Examples of what i am looking for
below url will go to standard 404 page
http://www.example.com/this-is-bad.html
below url will go to it's .html page
http://www.example.com/this-is-bad
below url will not redirect, because of slash at end
http://www.example.com/foobar/
below url is working and SHOULD NOT get redirected anywhere. This is not a file but a dynamic page.
http://www.example.com/this-is-good
Thanks
Just use ErrorDocument 404:
ErrorDocument 404 /foobar.html
If you want full redirect (change URL in broser) then useL
ErrorDocument 404 http://www.example.com/foobar.html
This sill redirect all 404 URLs to /foobar.html
UPDATE: Try this rule
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/?$ /$1.html [L,R=302]

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 url-rewrite if file not exists

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

Resources