Can anyone with a better grasp of the Apache code than I have explain to me why the following snippet from my .htaccess file is not doing what it should be doing?
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^filename.html$ [NC]
RewriteCond %{HTTP_REFERER} ^http://referring.webpage.com.*$ [NC]
RewriteRule ^.*$ http://www.wallsofthecity.net/year/month/filename.html [L]
</IfModule>
It redirects just fine, but then gets stuck in an infinite loop, since I am redirecting back to my own domain. However, the first RewriteCond should prevent the recursiveness… but according to this useful tool, it is not.
Help?









I hate mod rewrite
I think you need to swap the rules (put the referring web page “test” first), and then add C or chain as a flag to that first RewriteCond such that both of them need to be true to allow the RewriteRule to be applied.
You only want the URL to be rewritten if the request is coming from the off-site page, right? So if someone follows a link TO your site FROM peta.org, then you can redirect them to your “people eating tasty animals” page, right?
Either that or I don’t understand what you are trying to do.
Either that or I’m totally missing it in the documentation page.
I have a rather intense dislike for this functionality myself, especially since adding the “C” flag not only broke the script to a 500 error, but broke the entire webpage to a 500 error (how the hell does that work?), but in this case, it could be somewhat amusing.
And you got my intentions in one – if a link comes in from X source, I want it perpetually redirected to Y page, but I also do not want the request trapped in a recursive loop. The above code should do that. It does not, despite me spending an hour looking through various Apache help pages…
I think that the line
RewriteRule ^.*$ http://www.wallsofthecity.net/year/month/filename.html [L]
needs to be
RewriteRule ^.*$ /year/month/filename.html [L]
if you can, try using cURL from the command line to tell what is going on. Use the -I and -L flags and spoof your referer string with the -e flag. Or email me using the address at the footer of my blog and I’ll try curl from here (and also keep your URLs private).
Fortunately or unfortunately, the RewriteRule seems to be working perfectly… it is the RewriteCond that is supposed to break out of the infinite loop that is being a bitch. No way I have been able to phrase it seems to induce it to identify the page I am linking to as the page it needs to go to, and it just sits there and happily keeps redirecting folks to the same page, over and over and over again…
I will drop you an email, though… Really getting on my nerves
.
your linked-to web-based tool seems to be a front-end for the cURL command line tool, so you’re on the right track. Sent you an email back because I’m not clear of what you want to do.
hey, you only had to explain it to me twice
I send you a new code snippet after looking at some more examples of code.
Is it because you have a “.*$” immediately after the .com (no slash or space) on the RewriteCond line?
@Standard Mischief – Yeah, sorry for not making myself clear before… And if you did not get the email yet, it still fails.
@Reputo – Annoyingly, the redirect seems to work just fine. It is the line that is supposed to break it out of the recursive loop it gets into that fails miserably.
Here is the updated code we have now, that still does not work:
RewriteEngine onOptions +FollowSymlinks
RewriteBase /
RewriteCond %{REQUEST_URI} !^/year/mo/filename.html$ [NC]
RewriteCond %{HTTP_REFERER} ^http://online.webpage.com.* [NC]
RewriteRule (.*) http://www.wallsofthecity.net/year/mo/filename.html [L]
Do either of these links help? I’m not a programmer and haven’t worked with Apache before, but they might help. If they don’t, I would suggest creating an account with http://stackoverflow.com/ and asking the question there. You should have some helpful responses pretty quickly.
http://stackoverflow.com/questions/2784285/apache-htaccess-rewrite-rules-make-redirection-loop
http://stackoverflow.com/questions/3114509/htaccess-rewrite-rule-preventing-infinite-loop
Unfortunately, neither of those particular threads seemed to hold the answer, but thanks for pointing me towards the forum – a webpage dedicated to preventing stack overflows seems like a good thing indeed
. I will set myself up an account and see what answers I can get!
@Linoge – Great! I hope you find what you’re looking for there. A specific, well asked question will usually have answers provided within a few minutes. Your post here is a good basis for formatting the question on the site, just add what you’ve done so far to try to fix the problem. The site has a good community and are pretty helpful. I’m a junior sysadmin myself, so I prowl its sister site, Serverfault. In any case, I hope you figure it out!
Just put it up here: http://stackoverflow.com/questions/3137237/problems-breaking-an-htaccess-redirect-out-of-an-infinite-loop
Hopefully someone will be able to give me some pointers – I know it is something stupid and simple, but not knowing a damned thing about “regular expressions”, I have no idea what it might be.
Thanks again for pointing me that way. This is what I love about the internet – you do not have all the answers to all the questions, but you can get in touch with folks who do
.