homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

.htaccess Redirect if File Does Not Exist

Msg#: 4189469 posted 9:17 pm on Aug 19, 2010 (gmt 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?



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

Msg#: 4189469 posted 9:25 pm on Aug 19, 2010 (gmt 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.


Msg#: 4189469 posted 2:53 am on Aug 20, 2010 (gmt 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.


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

Msg#: 4189469 posted 3:11 am on Aug 20, 2010 (gmt 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.



Msg#: 4189469 posted 8:22 pm on Sep 7, 2010 (gmt 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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved