Googlebot- How to generate a 404 error status after a if statement

Hello, I have a multilingual site. Googlebot generates error 500 in many
case. Googlebot try any combination of parameters. It still tries to
test old urls of old websites I had in the past. To avoid this I decided
to create an if statement letting nobody enter with wrong parameters.
On one page for example I wrote this <% if params[:locale] == “en” ||
params[:locale] == “de” || params[:locale] == “fr”> (with other
parameters than en de or fr the site display itself not normally,
and with other parameters than en de fr there should be nothing to
display, as i have only a trilingual website. So if <% if
params[:locale] == “en” || params[:locale] == “de” || params[:locale] ==
“fr”, we can display the page
ELSE
I display a page with the 3 existing choices to click: fr en de. This
way I avoid a 500 error of googlebot or an anormally displayed page of a
google user clicking on a bad referenced page.

BUT the problem is that “google webmaster tools” indicates now that the
website has many errors. Because wrong parameters display a page without
error instead of returning a 404 error page. Because of this I have too
many pages to crawl, as i have infinite number of pages that returns
status 200 OK.

I just would like to write something that generates the 404 error page
status, in order googlebot do not references it.

So i copy all i found on the internet for scripts to generate a 404
error status, but nothing works.

Finally i put this html script

My app is capable to generate error status in many cases, as the page
/fr/not_found, does not exist, I get the correct error message on the
website, but for googlebot it is not a 404 status as the page exists.

How could I generate a 404 error status whenever I want and if possible
in a rhtml file?

Thanks a lot for your help

On Wed, Oct 24, 2012 at 3:07 PM, Pierre d. [email protected] wrote:

How could I generate a 404 error status whenever I want and if possible
in a rhtml file?

In the relevant controller method add:

    response.status= 404

Done.

Hassan S. wrote in post #1081027:

On Wed, Oct 24, 2012 at 3:07 PM, Pierre d. [email protected] wrote:

How could I generate a 404 error status whenever I want and if possible
in a rhtml file?

In the relevant controller method add:

    response.status= 404

Done.

Ok this is my controller:

class StoreController < ApplicationController
include ERB::Util

layout “store”

def index

begin
  page = StaticPage.find_by_id!(53)
  @text = page.text params[:locale]

rescue ActiveRecord::RecordNotFound
  flash[:notice] = t :page_not_found
  redirect_to index_url
end


@total, @products = build_list Histoire.scoped(:order=>"id DESC"),

{:field=>params[:field],:query=> params[:query]},nil,5

@cart = find_cart

@page_title = t(:stories)

end

I will add “response.status=404” in it. But what should I write in the
index.rhml in my if statement, instead of my "meta
http-equiv=“refresh”?:

<% if params[:locale] == “en” || params[:locale] == “de” ||
params[:locale] == “fr” %>

-display the html code of the page -

<%else%>

<%end%>

thank you for your help

On Thu, Oct 25, 2012 at 4:06 AM, Pierre d. [email protected] wrote:

def index

begin
  page = StaticPage.find_by_id!(53)
  @text = page.text params[:locale]

rescue ActiveRecord::RecordNotFound
  flash[:notice] = t :page_not_found
  redirect_to index_url
end

end

I will add “response.status=404” in it.

Uh, well. That won’t work if you’re doing a redirect; “redirect” means
“send a status 302”. You can’t do both.

But what should I write in the
index.rhml in my if statement, instead of my "meta
http-equiv=“refresh”?:

That said, I don’t understand why you’re redirecting to the same page;
why not just return a “not found” response and text? Though for this
locale issue, a 406 Not Acceptable might make more sense…

On Fri, Oct 26, 2012 at 9:41 AM, Pierre d. [email protected] wrote:

Could you help me implement this correctly in the controller? Do I have
to call a special page in the controller?

def whatever
if params[:good] == ‘yes’
render :good_result and return
else
response.status= 404
render :bad_result and return
end
end

if yes how do I execute this page, what should I write in the rhtml
statement?

Since you have 2 separate view files, good_result.html.erb and
bad_result.html.erb, you can put whatever’s appropriate in them.

Keep logic out of your views as much as possible.

Hello finally I put <%= response.status=404 %> after my if statement,
not in the controller but in the rhtml file, as I dont well know how to
deal with a controller.
the result is not bad as there is no redirect but it doesnt deliver an
true error status 404 or else, it just display 404 on the page.
Could you help me implement this correctly in the controller? Do I have
to call a special page in the controller?
Def
bla bla
end

if yes how do I execute this page, what should I write in the rhtml
statement?

:slight_smile: