How to prevent infinite redirect loop on htaccess

.htaccessrewrite

Our domain.com/app/index.php is loading domain.com/blog in an iframe.

So my redirect rules should be like;

- domain.com/              => /app/index.php
- domain.com/blog(.*)      => /app/index.php?(.*)

Second rule is there because if someone wants to go to blog directly, we run our app first and put the blog in iframe, and append incoming querystring to iframe src tag so that it opens not on it's homepage but on requested URL.

Problem is, since I am redirecting everything on my first rule, iframe ends up in infinite loop. It tries to putself inside another iframe within it's own iframe endlessly.

Is there a way to assign an Env var so that if redirection is made, it's not repeated ?

Or to clearly address the problem, I never want blog to open without the app. How should my htaccess be ?

Thanks,
D

ps: we want to do it on htaccess, since php+js redirections are inefficient and ugly.

Best Answer

You could investigate the RewriteCond directive for conditionally applying the RewriteRule. However this shouldn't be strictly necessary if your RewriteRule explicitly matches the beginning and end of the expected URL to redirect e.g.


RewriteRule ^/$ /app/index.php [L]

Note that the redirect can be internal or external. If you explicitly provide the [R] flag to your RewriteRule then the browser will be informed of the new URL to fetch. Otherwise Apache will attempt to fetch the rewritten rule internally without informing the browser of the actual URL fetched.

Lastly, you can use mod_rewrite directives (RewriteEngine, RewriteCond, RewriteRule) in your .htaccess file.

Related Topic