mod rewrite - .htaccess and SEO URLs - why is this an infinite loop? -


i have dirty url this: http://www.netairspace.com/photos/photo.php?photo=3392. want http://www.netairspace.com/photos/oh-ltu/finnair_airbus_330-202x/oul_efou_oulu/photo_3392/ (and later support short urls http://www.netairspace.com/pic/3392/ i'll leave out).

so have script photo_seo_url.php, takes photo id, builds seo url, , redirect (302 testing, 301 when i'm happy it). planned add .htaccess mod_rewrite rules on calling old url:

  • the old url rewritten internally photo_seo_url.php
  • photo_seo_url.php 301/302 redirect seo url
  • the seo url rewritten internally original photo.php

that way would, in theory, benefits of seo url while being able retire old ones @ leisure.

these rules used:

rewriteengine on rewriterule ^photos/.*/photo_([0-9]+)/?$ photos/photo.php?photo=$1 [nc,l] rewritecond %{query_string} photo=([0-9]+) rewriterule ^photos/photo\.php$ photos/photo_seo_url.php?photo=%1 [nc,l] 

but goes infinite redirect loop. why, if these 2 doing internal rewrites rather external redirects - or i'm missing?

i've solved problem adding new file showphoto.php, nothing include original photo.php, , changing line 2:

rewriteengine on rewriterule ^photos/.*/photo_([0-9]+)/?$ photos/showphoto.php?photo=$1 [nc,l] rewritecond %{query_string} photo=([0-9]+) rewriterule ^photos/photo\.php$ photos/photo_seo_url.php?photo=%1 [nc,l] 

but i'd still understand why original version goes infinite loop. i've missed or misunderstood something. approach sound?

to answer question, why loop occur? happens seo uri, get /photos/oh-ltu/finnair_airbus_330-202x/oul_efou_oulu/photo_3392/, say.

  • rule 1 fires converting get /photos/photo.php?photo=3392 triggers internal redirect restarts scan of .htaccessfile.

  • rule 2 fires converting get photos/photo_seo_url.php?photo=339 triggers internal redirect again restarts scan of .htaccessfile.

  • no further matches occur , hence passed script photos/photo_seo_url.php 302 /photos/oh-ltu/finnair_airbus_330-202x/oul_efou_oulu/photo_3392/ , browser detects redirection loop.

what need happen rule 1 firing prevent rule 2 firing after internal redirect. 1 way set environment variable, end (which gets converted redirect_end on next pass) , skip rules if set:

rewriteengine on rewritebase   /  rewriterule   ^photos/.*/photo_([0-9]+)/?$ photos/photo.php?photo=$1 [nc,e=end:1,l]  rewritecond   %{env:redirect_end}:%{query_string}   ^:photo=([0-9]+)$ rewriterule   ^photos/photo\.php$          photos/photo_seo_url.php?photo=%1 [nc,l] 

an alternative approach add dummy noredir parameter rewritten uri , add a:

rewritecond   %{query_string}   !\bnoredir 

to original second rule. however, photo.php need ignore this. hope helps :-)


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -