Majkson
Majkson

Reputation: 227

.htaccess - create 404 page without .html ending form

I would like to create a default 404 page on my website and I know how I can do it:

ErrorDocument 404 http://example.com/404.html

But how I can create a special redirect to this page from url: "http://example.com/page-not-found" ?? I searched in Google and here but I find nothing about it.

So finally, when user visit page: http://example.com/test-page and this url is not exist on my website, script should redirect user to page: "http://example.com/page-not-found" and this url should present html code from 404.html file.

Thanks.

Upvotes: 1

Views: 126

Answers (2)

Abhishek Gurjar
Abhishek Gurjar

Reputation: 7476

Try it like this in your root directory,

RewriteEngine on

RewriteBase /my_catalog/

# rewritten rule
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]

# not found rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ Page-not-found [R=301,L]

Page-not-found is your supposedly error html file if the incoming request is nor a file neither a directory in your server it will serve page-not-found which is internally rewritten to page-not-found.html.

Upvotes: 1

Ryan Terry
Ryan Terry

Reputation: 77

This can be accomplished with .htaccess and mod_rewrite.

Modify:

ErrorDocument 404 http://example.com/page-notfound.html

Then in your .htaccess add:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.html [NC,L]

This is of course, assuming you have mod_rewrite enabled.

Upvotes: 0

Related Questions