James
James

Reputation: 82136

PHP URL Rewriting

I am setting up some rewrite rules on an Apache server using mod_rewrite. I was wondering if it was possible to write a rule that will basically re-direct the user to the home page if the page is not found i.e.

http://example.com/test  <-- does not exist

However, I would like if the user was to navigate to this domain they are automatically re-directed to:

http://example.com/

With this in mind, I don't want the URL to still display "http://example.com/test" I would like the URL to update itself to become "http://example.com/".

Upvotes: 1

Views: 83

Answers (2)

Gumbo
Gumbo

Reputation: 655745

I wouldn’t use a HTTP redirection but instead send an error document together with the proper error status code.

Upvotes: 2

ceejayoz
ceejayoz

Reputation: 180147

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ http://domain.com/ [L,R=301]

Essentially, "if the requested filename is not a file or directory, redirect".

Upvotes: 3

Related Questions