Redirect 404 errors to another URL

Here’s a few lines I added to the .htaccess file on an Apache web server to redirect all “not-found” requests to another domain name. Useful if you want to have one domain point to another, but still be able to retain access to files only available on the first one.

RewriteEngine On
RewriteBase /
# index.html must be forwarded since access to / will redirect to /index.html (default page)
RewriteRule ^index.html$ http://example.com/ [L]
# if the file or directory with the requested name doesn't exist...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# forward the request by rewriting the domain name
RewriteRule ^(.*)$ http://example.com/$1 [L]
About these ads


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 45 other followers