N
N
neochar2016-09-04 18:28:06
Yii
neochar, 2016-09-04 18:28:06

How to implement htaccess proxy for link with working iframe and ajax requests?

Now the situation is as follows: there is an example.com domain and product pages, each available at its own address:
- example.com/product-one
- example.com/product-two
- etc

All these pages are processed by one Yii action (htaccess has a RewriteRule, which leads to index.php?r=sale/product&product_id=%SOME_ID%). Each product has a link with an html file uploaded to the server, which has relative paths and therefore this html file is loaded on the product page in the IFRAME. So resources are not lost with relative paths. I process events in this html through jQuery scripts written on the product page, there is validation and ajax requests.

Now the task arose to register a separate domain for each product. For example:
-product-one.com => example.com/product-one (or example.com/index.php?r=sale/product&id=1 )
- product-two.com => example.com/product-two (or example .com/index.php?r=sale/product&id=2 )
- etc

I write the rule like this:

RewriteCond %{HTTP_HOST} ^product-one.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) http://example.com/product-one [P]


The problem is that the IFRAME in this case is no longer loaded for browser security reasons (i.e. I am trying to load the contents of another domain into an iframe of one domain).
If I rewrite the rendering of the product-related html directly via echo (specifying the < base > tag so as not to lose relative paths), then an error appears:
XMLHttpRequest cannot load http://example.com/index.php?r=debug%2Fdefault%2Ftoolbar&tag=57cc3a8c22a51. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://detox-plus-monster.local' is therefore not allowed access.


There is an idea to try passing a separate domain as a parameter in index.php Yii2 and dynamically specify baseUrl in the settings, but I don’t know how to do it. For this decision a separate plus!

How can this issue be resolved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
neochar, 2016-09-04
@neochar

Okay, so far I've only found how to change the baseUrl from the controller so that it doesn't throw AJAX errors due to security issues.
1. I pass the baseUrl = product-one.com GET parameter to htaccess 2.
I write in the controller:

public function actionProduct($id, $baseUrl = null)
    if (!empty($baseUrl)
        Yii::$app->set('urlManagerFrontend', [
            'class' => 'yii\web\UrlManager',
            'baseUrl' => $baseUrl,
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
        ]);
    }
}

So far this is a working solution. But I'd still appreciate any more ideas!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question