Example #1
0
 /**
  * Perform custom url rewrites
  *
  * @param \Magento\Framework\App\RequestInterface $request
  * @return bool
  */
 public function rewrite(\Magento\Framework\App\RequestInterface $request = null)
 {
     if (!$this->_appState->isInstalled()) {
         return false;
     }
     if (is_null($this->getStoreId()) || false === $this->getStoreId()) {
         $this->setStoreId($this->_storeManager->getStore()->getId());
     }
     /**
      * We have two cases of incoming paths - with and without slashes at the end ("/somepath/" and "/somepath").
      * Each of them matches two url rewrite request paths - with and without slashes at the end
      * ("/somepath/" and "/somepath").
      * Choose any matched rewrite, but in priority order that depends on same presence of slash and query params.
      */
     $requestCases = array();
     $pathInfo = $request->getPathInfo();
     $origSlash = substr($pathInfo, -1) == '/' ? '/' : '';
     $requestPath = trim($pathInfo, '/');
     // If there were final slash - add nothing to less priority paths. And vice versa.
     $altSlash = $origSlash ? '' : '/';
     $queryString = $this->_getQueryString();
     // Query params in request, matching "path + query" has more priority
     if ($queryString) {
         $requestCases[] = $requestPath . $origSlash . '?' . $queryString;
         $requestCases[] = $requestPath . $altSlash . '?' . $queryString;
     }
     $requestCases[] = $requestPath . $origSlash;
     $requestCases[] = $requestPath . $altSlash;
     $this->loadByRequestPath($requestCases);
     $targetUrl = $request->getBaseUrl();
     /**
      * Try to find rewrite by request path at first, if no luck - try to find by id_path
      */
     if (!$this->getId() && isset($_GET['___from_store'])) {
         try {
             $fromStoreId = $this->_storeManager->getStore($_GET['___from_store'])->getId();
         } catch (\Exception $e) {
             return false;
         }
         $this->setStoreId($fromStoreId)->loadByRequestPath($requestCases);
         if (!$this->getId()) {
             return false;
         }
         $currentStore = $this->_storeManager->getStore();
         $this->setStoreId($currentStore->getId())->loadByIdPath($this->getIdPath());
         $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear();
         $this->_cookieManager->setPublicCookie(\Magento\Store\Model\Store::COOKIE_NAME, $currentStore->getCode(), $cookieMetadata);
         $targetUrl .= '/' . $this->getRequestPath();
         $this->_sendRedirectHeaders($targetUrl, true);
     }
     if (!$this->getId()) {
         return false;
     }
     $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $this->getRequestPath());
     $external = substr($this->getTargetPath(), 0, 6);
     $isPermanentRedirectOption = $this->hasOption('RP');
     if ($external === 'http:/' || $external === 'https:') {
         $destinationStoreCode = $this->_storeManager->getStore($this->getStoreId())->getCode();
         $cookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear();
         $this->_cookieManager->setPublicCookie(\Magento\Store\Model\Store::COOKIE_NAME, $destinationStoreCode, $cookieMetadata);
         $this->_sendRedirectHeaders($this->getTargetPath(), $isPermanentRedirectOption);
     } else {
         $targetUrl .= '/' . $this->getTargetPath();
     }
     $isRedirectOption = $this->hasOption('R');
     $isStoreInUrl = $this->_scopeConfig->getValue(\Magento\Store\Model\Store::XML_PATH_STORE_IN_URL, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     if ($isRedirectOption || $isPermanentRedirectOption) {
         if ($isStoreInUrl && ($storeCode = $this->_storeManager->getStore()->getCode())) {
             $targetUrl .= '/' . $storeCode . '/' . $this->getTargetPath();
         }
         $this->_sendRedirectHeaders($targetUrl, $isPermanentRedirectOption);
     }
     if ($isStoreInUrl && ($storeCode = $this->_storeManager->getStore()->getCode())) {
         $targetUrl .= '/' . $storeCode . '/' . $this->getTargetPath();
     }
     $queryString = $this->_getQueryString();
     if ($queryString) {
         $targetUrl .= '?' . $queryString;
     }
     $request->setRequestUri($targetUrl);
     $request->setPathInfo($this->getTargetPath());
     return true;
 }