Joe
Joe

Reputation: 2452

.htaccess - Redirect with the following rules / conditions?

How would a rule matching the following look like:

Redirect all non-file requests to root (example.com/foo or example.com/foo/goo to example.com) Redirect all non-existing file requests to root (example.com/not_exisiting_file.php to example.com) Pass all requests to existing files.

How would a mod-rewrite in htaccess look like?

Thanks

Upvotes: 0

Views: 41

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

You can use the following rule in your /.htaccess file to redirect all non-existing file requests to the root:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ http://example.com/ [R,NC,L] 

Upvotes: 0

Graham Dumpleton
Graham Dumpleton

Reputation: 58523

For Apache 2.4, see if the FallbackResource directive will do what you want.

It is intended to supersede the previous use of a RewriteCond with -d and -f.

Upvotes: 1

Related Questions