Help needed External 301 redirect when folder doesn't exist

Discussion in 'Apache' started by texastez, May 17, 2010.

  1. #1
    I have the following in my htacces file

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(accommodation\.php)?\?id=([0-9]+)(#[^\ ]*)?\ HTTP/
    RewriteRule ^(accommodation\.php)?$ http://www.example.com/%2? [R=301,L]

    RewriteRule ^([0-9]+)$ accommodation.php?id=$1 [L]

    The above works fine and produced the desired result (using id 89 as example):
    http://www.example.com/89 instead of the old result http://www.example.com/accommodation.php?id=89
    and broswers redirct work fine too!

    What we are trying to do, rather than just have the url say: http://www.example.com/89 is to insert the type of property it actually is into the url something like: http://www.example.com/Apartment/89

    NEW REWRITE RULE:
    The new rewrite rule has been changed to:
    RewriteRule ^([A-Z][a-z]+)/([0-9]+)$ accommodation.php?accommodation_type$1&id=$2 [L]

    Using the 'accommodation type' from the 'accommodation_type' field in the database.
    The new rewrite rule above works fine for internal links within the site returning: http://www.example.com/Apartment/89
    or
    http://www.example.com/House/89
    or
    whatever the accommodation_type is in the database.

    The php code is like this...<a href='".$row[accommodation_type]."/".$row["id"]."'> for links within the site.

    But I can not get the external 301 redirect part - this...

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(accommodation\.php)?\?id=([0-9]+)(#[^\ ]*)?\ HTTP/
    RewriteRule ^(accommodation\.php)?$ http://www.example.com/%2? [R=301,L]

    ...to work!

    It seem as though something needs to be added between example.com/HERE! AND/%2?
    Is it seeing the 'accommodation_type' as a folder that doesn't exist.

    Thanks!
     
    texastez, May 17, 2010 IP
  2. Namjies

    Namjies Peon

    Messages:
    315
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could simply use

    
    RewriteCond %{QUERY_STRING}  ^id=([0-9]+)$
    RewriteRule ^accommodation\.php$ http://www.example.com/%1? [R=301,L]
    
    RewriteRule ^([0-9]+)$ accommodation.php?id=$1 [L]
    
    Code (markup):
    Or for the accomodation type add extra variables/format to the QUERY_STRING condition and then using these % in another redirecting rewrite
    (don't forget to use [or] and have all possible order combinations for the variables in the QUERY_STRING as multiple possible RewriteCond)

    You don't need to use the full request to get your variables.
     
    Namjies, May 19, 2010 IP