Kiavor
Kiavor

Reputation: 474

.htaccess url-rewrite if file not exists

I must do a little trick for a site! The idea is:

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?

Upvotes: 32

Views: 48765

Answers (3)

anubhava
anubhava

Reputation: 784958

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).

Upvotes: 10

Vladimir
Vladimir

Reputation: 159

Implement a 404 error rule. Doesn't require mod_rewrite:

ErrorDocument 404 /default.php

Upvotes: 5

user237419
user237419

Reputation: 9064

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

Upvotes: 81

Related Questions