Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

.htaccess Redirect if File Does Not Exist

9:17 pm on Aug 19, 2010 (gmt 0)

New User

5+ Year Member

joined:June 16, 2010
votes: 0

I have a group of files something like this:


Some exist, some don't... I'm trying to set up a redirect so the ones that don't exist are sent to my search page and the ones that do exist aren't modified at all. I've got this semi-working, except it sends the user to my live directory(www.example.com) instead of my development directory(dev.example.com):

#RewriteCond %{REQUEST_FILENAME} !-f
#RedirectMatch /test-(.*).html search.example.com/search/test-$1 [R=301]

So my question is, is there a better way to do this? If not, how do I make it stay on the development directory and not send me to the live one?
9:25 pm on Aug 19, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
votes: 0

For the ones that don't exist, send a 404 response and present a search box on the 404 error message page.

Redirecting to another URL is not the correct action for the pages that do not exist.
2:53 am on Aug 20, 2010 (gmt 0)

New User

5+ Year Member

joined:June 16, 2010
votes: 0

That isn't a feasible option for me. I have thousands and thousands of pages linked within the site that would take too many man hours to fix. Redirecting to our search is the best option for us at this time.
Most of the pages that don't exist anymore are because they were renamed and our learning search will find them as well as show the related pages.
3:11 am on Aug 20, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
votes: 0

The closest you can get to technically-correct with a redirect would be:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^test-([^.])+\.html http://dev.example.com/search/test-$1 [R=302,L]

I show a 302 redirect, because a 301 would be "SEO suicide."

A *much* better solution if you care about search engine rankings would be to use the default 404 response for those non-existent pages, and to either #include the search function on the custom 404 error page, or to use the search page itself as the custom 404 error page:

ErrorDocument 404 /search.php

However, the "search.php" script will now have to get the originally-requested URI from the REQUEST_URI server variable, as that information is no longer being passed to it by a rewrite or redirect.

Because the server status response with this configuration is a 404-Not Found, you avoid trouble with search engines detecting an "infinite URL-space" or flagging massive duplicate-content on your site, and users see a nice search-facility page with no jarring NOT FOUND messages... Best of both worlds.

8:22 pm on Sept 7, 2010 (gmt 0)

New User

5+ Year Member

joined:Sept 7, 2010
posts: 1
votes: 0

I have a Wordpress Multisite website and i want any url that does not exist to redirect to my main website.

for example, if mysite.com/test does not exist i want it to redirect to myofficialsite.com

I don't care for SEO on this website.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members