1313use Magento \Framework \HTTP \PhpEnvironment \Response ;
1414use Laminas \Http \Response \Stream ;
1515use Magento \UpwardConnector \Model \Prerender ;
16+ use Magento \Store \Model \StoreManagerInterface ;
17+ use Magento \Framework \UrlInterface ;
18+ use Magento \Framework \App \Config \ScopeConfigInterface ;
19+ use Magento \Store \Model \ScopeInterface ;
1620
1721class Upward implements FrontControllerInterface
1822{
@@ -31,20 +35,44 @@ class Upward implements FrontControllerInterface
3135 */
3236 private $ prerender ;
3337
38+ /**
39+ * @var StoreManagerInterface
40+ */
41+ private $ storeManager ;
42+
43+ /**
44+ * @var \Magento\Framework\UrlInterface
45+ */
46+ private $ url ;
47+
48+ /**
49+ * @var \Magento\Framework\App\Config\ScopeConfigInterface
50+ */
51+ private $ scopeConfig ;
52+
3453 /**
3554 * Upward constructor.
3655 * @param Response $response
3756 * @param UpwardControllerFactory $upwardFactory
3857 * @param Prerender $prerender
58+ * @param StoreManagerInterface $storeManager
59+ * @param UrlInterface $url
60+ * @param ScopeConfigInterface $scopeConfig
3961 */
4062 public function __construct (
4163 Response $ response ,
4264 UpwardControllerFactory $ upwardFactory ,
43- Prerender $ prerender
65+ Prerender $ prerender ,
66+ StoreManagerInterface $ storeManager ,
67+ UrlInterface $ url ,
68+ ScopeConfigInterface $ scopeConfig
4469 ) {
4570 $ this ->response = $ response ;
4671 $ this ->upwardFactory = $ upwardFactory ;
4772 $ this ->prerender = $ prerender ;
73+ $ this ->storeManager = $ storeManager ;
74+ $ this ->url = $ url ;
75+ $ this ->scopeConfig = $ scopeConfig ;
4876 }
4977
5078 /**
@@ -55,6 +83,24 @@ public function __construct(
5583 */
5684 public function dispatch (RequestInterface $ request )
5785 {
86+ $ baseUrl = $ this ->storeManager ->getStore ()->getBaseUrl (
87+ UrlInterface::URL_TYPE_WEB ,
88+ $ this ->storeManager ->getStore ()->isCurrentlySecure ()
89+ );
90+ // phpcs:disable Magento2.Functions.DiscouragedFunction
91+ $ uri = parse_url ($ baseUrl );
92+ if ($ request ->getUri ()->getHost () !== $ uri ['host ' ]) {
93+ $ redirectUrl = $ this ->url ->getRedirectUrl (
94+ $ this ->url ->getDirectUrl (ltrim ($ request ->getPathInfo (), '/ ' ), ['_nosid ' => true ])
95+ );
96+ $ redirectCode = (int )$ this ->scopeConfig ->getValue (
97+ 'web/url/redirect_to_base ' ,
98+ ScopeInterface::SCOPE_STORE
99+ ) !== 301 ? 302 : 301 ;
100+ $ this ->response ->setRedirect ($ redirectUrl , $ redirectCode );
101+ return $ this ->response ;
102+ }
103+
58104 $ prerenderedResponse = null ;
59105 if ($ this ->prerender ->shouldShowPrerenderedPage ($ request )) {
60106 /** @var \Laminas\Http\Response $prerenderedResponse */
0 commit comments