Forum Moderators: phranque

Message Too Old, No Replies

Redirecting broken urls to frontpage?

Using modrewrite to create URLs too

         

JohnnyDoomo

4:38 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



My site is www.example.com and sometimes I have to remove games from my arcade on the request of the owners. One example is this url: http://www.example.com/games/play-Kennel_Chaos

The script I use to run the site doesn't put anything in there once a game has been removed. I would like to know how to get it so that this page and any others I specify will return users to the frontpage ( http://www.example.com/games/index.php )

Can somebody please help me accomplish this?

Thanks for any help you can provide!

[edited by: jdMorgan at 6:28 pm (utc) on Mar. 4, 2008]
[edit reason] No URLs, please. See Terms of Service. [/edit]

gergoe

6:17 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



Let's start with a quick clarification; You have to separate the physical files within your host's file system, and the urls used by your website, they are not the same. The first ones are handled by the Apache, it can find out that a certain file exists or not, while the second one is only resolved to actual files after the modules in Apache has parsed the request, especially talking about the mod_rewrite module. For example if you are rewriting urls like www.example.com/samples/sample12 into script.php?sample=sample12, then apache will do it's job, and unless the script.php file can not be found, it will not give any error back to the browser

If your website has an url like mentioned above (and you seem to have), and this is not a physical file within the file system but a dynamically generated url (and later rewritten by mod_rewrite to a physical filename), then Apache will not know nothing about it's status, can't find out that it exists or not. In such a case, your server-side script has to do the error handling, if the request is for a non-existing resource, then it should send back a 404 status code, with some explanation on the same webpage why it happens (or give suggestions where to look for a 'replacement'). This could look similar to your homepage, but it is strongly advised to avoid redirecting missing resources to your homepage just like that (SEO considerations).

JohnnyDoomo

8:34 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



Well the page is no longer in the database, so there is no way to actually get to the http://www.example.com/games/play-Kennel_Chaos page. Meaning that it would have to already exist in a search engine or some site was linking directly to that page. It does use mod rewrite, but shouldn't it still be possible to tell the server that is someone is trying to go to that exact page of http://www.example.com/games/play-Kennel_Chaos that instead of using the script at all, it redirects them to my frontpage? There are no links on my site that direct to http://www.example.com/games/play-Kennel_Chaos as it has been removed from the script, but if I had to remove the game and another site was linking directly to that page, my stats show a lot of people hitting pages that are pages that show errors on them because I had to remove the main embedded file for the page.

I simply want to take those people going to those error invested pages to the front page so I can hopefully salvage some visitors from those hits I'm getting.

Is this possible? I don't know a thing about htaccess and need help on exactly what I should add to my htaccess if this is possible. You are right that those paths to actually exist on the server. There is not a folder called http://www.example.com/games/play-Kennel_Chaos and that it is all put together by the script on a page using template files that are used for all the media that exists on my site.

I will stop worrying about the problem if I know that htaccess cannot fix this problem and get visitors redirecting to my frontpage, but if there's a chance it will work, I'd like to try.

Thanks for any help you can provide.

gergoe

9:20 pm on Mar 4, 2008 (gmt 0)

10+ Year Member



Of course you can do that with .htaccess, but note the last sentence of my previous post, providing an "all went fine" response for a resource which does not exists is confusing. Moreover issuing a redirect for a non existent resource means that url_to_have_redirected is being replaced (temporary or permanently) by url_to_redirect_to, and this is not your case at all.

So to get back to the htaccess way, you can do the following in a .htaccess file in your root:


Opetions +FollowSymLinks
RewriteEngine On
RewriteRule ^games/play-Kennel_Chaos/?^ http://www.example.com/ [R=301,L]

If you prefer to do it in a htaccess file, then you have to list each entry separately. If you ask me, implement this functionality in your script, it must be a matter of one or two lines...

JohnnyDoomo

12:05 am on Mar 5, 2008 (gmt 0)

10+ Year Member



I tried adding that code and it made all my games to the frontpage after it used my meta redirect that I have for a 401 error.

Screw it, it's not going to work probably anyways.

Thanks anyways. I can't change the script, because I don't know coding and it's a script that I purchased that runs my site. I would have no idea where to look or what to add.

gergoe

12:33 am on Mar 5, 2008 (gmt 0)

10+ Year Member



There were two typos in it (at once; is there no award for that?), you can try this one:

Options +FollowSymLinks 
RewriteEngine On
RewriteRule ^games/play-Kennel_Chaos/?$ http://www.example.com/ [R=301,L]

Alternatively you can use the mod_alias Redirect directive [httpd.apache.org] which would have the following syntax:

Redirect permanent /games/play-Kennel_Chaos/ http://www.example.com/

But keep in mind that it is generally a bad idea to redirect instead of returning an error status; when something can not be found, you have to tell the user agent (browser or robot) that it can not be found. You search rankings may be affected by ignoring such a thing.

g1smd

1:06 am on Mar 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This error handling is not a matter for .htaccess.

It is best done by modifying your script:

IF (item not found in datebase) then DO (send error code in HTTP header and send error page content).