how to make a 301 redirect with rewrite if page dont exist

Discussion in 'Apache' started by tracy.lyricy, Nov 1, 2009.

  1. #1
    tracy.lyricy, Nov 1, 2009 IP
  2. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #2
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*) /sarasa/sarasa.html
    Code (markup):
    If the directory or file does not exist then it uses /sarasa/sarasa.html

    However be careful as the content of /sarasa/sarasa.html will show for every unknown URL and return that the page is present (200 status code). At a guess I'd imagine this will cause havoc with search engines.

    A custom 404 page may be a better idea:

    ErrorDocument 404 /sarasa/sarasa.html

    That would show the page you want but return a 404 status code so search engines etc know the page isn't present but visitors still see a nice page to select where to go next.
     
    tolra, Nov 1, 2009 IP
    tracy.lyricy likes this.
  3. tracy.lyricy

    tracy.lyricy Member

    Messages:
    163
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #3
    dear @tolra thanks.. a lot.

    Let me explain what I'm trying to do. I have many sites that link to my site, this sites sometimes link to incorrect pages, for example: www.sites.com/sarasa/onepage.html and maybe onepage.html doesn't exist so I want to redirect in a 301 way to www.site.com/sarasa/sarasa.html. I that way I don't loose pr...
    For other case I'll use the custom 404 page...

    this script cover that? if not could you please, help again? :D

    thanks a lot,
     
    tracy.lyricy, Nov 2, 2009 IP
  4. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #4
    Actually I made a typo, my apologies, it should have been:
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*) /sarasa/sarasa.html [L,R=301]
    
    Code (markup):
    That should 301 any missing page to a single given page, in this case /sarasa/sarasa.html, you should never get a 404.

    No idea how search engines will view this, but should be interesting to find out :)
     
    tolra, Nov 3, 2009 IP