I need help to redirect this links to 404 error

Discussion in 'PHP' started by alfa_375, Jul 14, 2013.

  1. #1
    If someone tries to access these abbreviated page-types, then a 404 error:

    http://getcityweather.com/international/region/na.html
    http://getcityweather.com/international/ca/forecasts.html
    http://getcityweather.com/international/ca/al_forecasts.html
    http://getcityweather.com/international/ca/alberta_forecasts.html

    I had tried to use .htaccess file redirect
    # redirect all non-www traffic
    #RewriteCond %{HTTP_HOST} ^region/\?\?\.html$
    #RewriteRule ^region/??\.html$  http://www.google.com [R=permanent,L]
    #RewriteRule ^region/([A-Za-z]{2})\.html$    http://www.google.com [R=permanent,L]
     
    or this below code
     
    Redirect 301 /region/af.html  http://www.getcityweather.com/us/
    
    PHP:
    Some one please let me know, whether I should do it through .htaccess way
    or is there anything in php like catching the url and redirecting them.

    Please help it is little urgent.
     
    alfa_375, Jul 14, 2013 IP
  2. EmmanuelFlossie

    EmmanuelFlossie Active Member

    Messages:
    159
    Likes Received:
    11
    Best Answers:
    2
    Trophy Points:
    65
    #2
    My knowledge of a rewrite rule is very limited, so I would Redirect 301

    Maybe somebody in this forum will know how to do it with the rewrite rules.
     
    EmmanuelFlossie, Jul 14, 2013 IP
  3. alfa_375

    alfa_375 Active Member

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #3
    It is ok! If you can provide me the redirect 301. Tell me, how would you do that?
     
    alfa_375, Jul 14, 2013 IP
  4. EmmanuelFlossie

    EmmanuelFlossie Active Member

    Messages:
    159
    Likes Received:
    11
    Best Answers:
    2
    Trophy Points:
    65
    #4
    Well your example was correct

    in your .htaccess file type
    
    Redirect 301 /region/na.html http://www.getcityweather.com/us/
    
    Code (markup):
     
    EmmanuelFlossie, Jul 14, 2013 IP
  5. alfa_375

    alfa_375 Active Member

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #5
    But the problem is, it is not working that is why I am asking for help here.
     
    alfa_375, Jul 14, 2013 IP
  6. EmmanuelFlossie

    EmmanuelFlossie Active Member

    Messages:
    159
    Likes Received:
    11
    Best Answers:
    2
    Trophy Points:
    65
    #6
    did you upload it to the root folder? and btw your htaccess is directing the wrong file

    if you look at mine i have added /region/na.html while you have done /region/af.html
     
    EmmanuelFlossie, Jul 14, 2013 IP
  7. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #7
    Is mod_rewrite even enabled? And you are actually using Apache as a webserver?
     
    ryan_uk, Jul 14, 2013 IP
  8. alfa_375

    alfa_375 Active Member

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Yes, it is in root folder cause there are other stuff, which are working fine.

    Actually, I am looking for both type of files na.html or af.html, in fact I like to have here any two characters of the file.html
    ab.html
    ac.html
    na.html
    sa.html

    All these type of files should go to the 404 error page.

    Thanks
     
    Last edited: Jul 14, 2013
    alfa_375, Jul 14, 2013 IP
  9. alfa_375

    alfa_375 Active Member

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #9
    Yes mode_rewrite is enabled as my other stuff is working fine.
     
    alfa_375, Jul 14, 2013 IP
  10. alfa_375

    alfa_375 Active Member

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #10
    Here is the other content of my file:
    
    Options +FollowSymLinks
    RewriteEngine On
    ######################
    #  Weather Directory
    ######################
     
    Redirect 301 /region/af.html  http://www.getcityweather.com/us/
     
    #display regions
    RewriteRule ^forecasts\.html$  /international/dir_files/index.php?do=regions    [L,S=1]
     
    #display countries in a region
    RewriteRule ^region/([A-Za-z][A-Za-z+]+)\.html$  /international/dir_files/index.php?do=countries&r=$1    [L,S=1]
     
    #display cities or states in a country
    RewriteRule ^([A-Za-z+]+)/forecasts\.html$    /international/dir_files/index.php?do=states&c=$1    [L,S=1]
    
    Code (markup):
     
    alfa_375, Jul 14, 2013 IP
  11. Luis Jimenez

    Luis Jimenez Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #11
    Also, make sure you have the AllowOverride directive set up for the document root of your website.

    http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride
     
    Luis Jimenez, Jul 14, 2013 IP
  12. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #12
    So, you want to redirect all /region/xx.html?

    RewriteRule ^region/(.*)\.html$ /us/ [NC,R,L]
    Code (markup):
     
    ryan_uk, Jul 15, 2013 IP
  13. sumarjan

    sumarjan Active Member

    Messages:
    129
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #13
    why you are not using redirect 301, or redirect to 404 page using this meta header response

    <?php
    header("HTTP/1.0 404 Not Found");
    ?>
     
    sumarjan, Jul 15, 2013 IP
  14. Nick Son

    Nick Son Member

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    41
    #14
    I think this is the best solution for you. You can use it in your 404 page. So it may redirect all error link.
    http://www.osproseo.com/redirect-301-permanently-php/
     
    Nick Son, Jul 15, 2013 IP
  15. alfa_375

    alfa_375 Active Member

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #15
    Thanks guys for your response, I had sort out this thing by Mr. google.
     
    alfa_375, Jul 16, 2013 IP
  16. alfa_375

    alfa_375 Active Member

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #16
    Oh here is my code for this file, which is working like a charm.
    
    RewriteEngine On
     
    RewriteRule (^region/[A-Za-z]{2}\.html) - [F,L] ###/asdf [R,L]
    
    Code (markup):
    Once again thanks dpiers for your time for providing such valuable help. I want to set best answer but I don't want to others to discourage for their efforts.
     
    alfa_375, Jul 16, 2013 IP