user198989
user198989

Reputation: 4663

Htaccess 404 - redirect

If someone requests a page that doesnt exist, for example if its

site.com/blabla/?search=blabla

with using this htacess

ErrorDocument 404 /

They're being redirected to homepage with the same url, site.com/blabla/?search=blabla What I want to do is, to redirect them to an IP, instead of the homepage. Tried this;

ErrorDocument 404 http://64.233.185.94/

But it just redirects to http://64.233.185.94/ I want to redirect it with the not found url, so it should redirect to

http://64.233.185.94/blabla/?search=blabla

at this point.

What is the correct way to do that ?

Upvotes: 1

Views: 95

Answers (2)

anubhava
anubhava

Reputation: 786271

You will need to use a mod_rewrite rule instead of ErrorDocument for that:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !=64.233.185.94
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ http://64.233.185.94/$1 [L,R]

Upvotes: 1

Sumurai8
Sumurai8

Reputation: 20755

You can use mod_rewrite to accomplish this. With mod_rewrite you can test if a requested file or directory exists, and if they do not exist you can do an external redirect:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILNEMAE} !-d
RewriteRule ^ http://64.233.185.94%{REQUEST_URI} [R,L]

Change the R flag to R=301 after testing that this works as expected.

Upvotes: 1

Related Questions