JensOlsen112
JensOlsen112

Reputation: 1299

If specific URL not found - 301 to root

Basically I'm trying to edit my .htaccess file to do the following: I want to 301 a URL to another URL but ONLY if that URL is not found (404).

Thus the following will not suffice, seeing as it will redirect the URL regardless of whether or not the URL was found.

Redirect 301 /oldpage.html http://www.example.com/newpage.html

Is this possible to do through .htaccess?

And yes, I know this might be an odd request but I have my reasons for needing this.

Upvotes: 1

Views: 54

Answers (2)

symcbean
symcbean

Reputation: 48367

You really don't want to 301 a URL to another URL. Permanent redirects cause all sorts of problems which cannot be reverted.

I want to 301...if that URL is not found (404).

Again, that's a really bad idea. Even if your deployment and testing processes are perfect you need to be able to see and respond to the requests coming in.

Use an error document to display a message with a meta redrect after a delay to bounce the user back to the home page.

Upvotes: 0

anubhava
anubhava

Reputation: 785481

Try this mod_rewrite rule in your root .htaccess:

RewriteEngine On

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^oldpage\.html http://www.example.com/newpage.html [L,NC,R=302]

Upvotes: 1

Related Questions