Skip to content

Commit

Permalink
Fix 'Status code is 302 instead of 301 #10'
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-79 committed Mar 22, 2023
1 parent e19b448 commit d7cf39e
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 18 deletions.
32 changes: 30 additions & 2 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,44 @@
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
const XML_PATH_HIDE_DEFAULT_STORE_CODE = 'web/url/hide_default_store_code';
const XML_PATH_REDIRECT_TO_URL_WITHOUT_STORE_CODE = 'web/url/redirect_to_url_without_store_code';

const NO_REDIRECT = 0;
const PERMANENT_REDIRECT = 301;
const TEMPORARY_REDIRECT = 302;

/**
*
* @return boolean
*/
public function isHideDefaultStoreCode()
{
if ($this->scopeConfig->getValue(self::XML_PATH_HIDE_DEFAULT_STORE_CODE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
if ($this->scopeConfig->getValue(
self::XML_PATH_HIDE_DEFAULT_STORE_CODE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)) {
return true;
}
return false;
}

/**
* @return int
*/
public function getRedirectCode()
{
$code = $this->scopeConfig->getValue(
self::XML_PATH_REDIRECT_TO_URL_WITHOUT_STORE_CODE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);

if ($code == 301) {
return self::PERMANENT_REDIRECT;
}

if ($code == 1) {
return self::TEMPORARY_REDIRECT;
}

return self::NO_REDIRECT;
}
}
16 changes: 6 additions & 10 deletions Observer/RedirectWithoutStoreCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

class RedirectWithoutStoreCode implements \Magento\Framework\Event\ObserverInterface
{
/**
* @var \Magento\Framework\App\Response\RedirectInterface
*/
protected $redirect;

/**
* @var \Magento\Framework\App\ActionFlag
*/
Expand All @@ -31,20 +26,17 @@ class RedirectWithoutStoreCode implements \Magento\Framework\Event\ObserverInter

/**
*
* @param \Magento\Framework\App\Response\RedirectInterface $redirect
* @param \Magento\Framework\App\ActionFlag $actionFlag
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Noon\HideDefaultStoreCode\Helper\Data $helper
* @param \Magento\Framework\UrlInterface $url
*/
public function __construct(
\Magento\Framework\App\Response\RedirectInterface $redirect,
\Magento\Framework\App\ActionFlag $actionFlag,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Noon\HideDefaultStoreCode\Helper\Data $helper,
\Magento\Framework\UrlInterface $url
) {
$this->redirect = $redirect;
$this->actionFlag = $actionFlag;
$this->storeManager = $storeManager;
$this->helper = $helper;
Expand All @@ -65,11 +57,15 @@ public function execute(\Magento\Framework\Event\Observer $observer)
$url = $this->url->getCurrentUrl();
$pos = strpos($url, $this->storeManager->getStore()->getBaseUrl() . $defaultStore->getCode());

if ($this->helper->isHideDefaultStoreCode() && $pos !== false) {
if (
$this->helper->isHideDefaultStoreCode() &&
$pos !== false &&
$code = $this->helper->getRedirectCode()
) {
$controller = $observer->getData('controller_action');
$url = str_replace('/' . $defaultStore->getCode() . '/', '/', $url);
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->redirect->redirect($controller->getResponse(), $url);
$controller->getResponse()->setRedirect($url, $code);
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# HideDefaultStoreCode

Magento 2 plugin for hide *Default Store Code* from URL.
Magento 2 module for hide *Default Store Code* from URL.

<https://bender.kr.ua/howto-hide-default-store-code-from-url-magento-2/>

![howto-hide-default-store-code-from-url-magento-2](https://bender.kr.ua/img/howto-hide-default-store-code-from-url-magento-2-1-1.png)
![howto-hide-default-store-code-from-url-magento-2](doc/img/hide-default-store-code-from-url-magento-2-1-1.png)

***

## Installation
## INSTALLATION

### manual

Expand All @@ -28,6 +28,12 @@ composer require noon/hide-default-store-code
php bin/magento setup:upgrade
```

## Configuration
## CONFIGURATION

### ENABLE/DISABLE

*Stores > Configuration > General > Web > Url Options > Hide Default Store Code*

### REDIRECT

*Stores > Configuration > General > Web > Url Options > Auto-redirect to URL without Store Code*
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"magento/module-backend": ">=100.0"
},
"type": "magento2-module",
"version": "1.0.4",
"version": "1.0.5",
"autoload": {
"files": [
"registration.php"
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<label>Hide Default Store Code</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="redirect_to_url_without_store_code" translate="label comment" type="select" sortOrder="101" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Auto-redirect to URL without Store Code</label>
<source_model>Magento\Config\Model\Config\Source\Web\Redirect</source_model>
<comment>I.e. redirect from http://example.com/store/ to http://example.com/</comment>
<depends>
<field id="web/url/hide_default_store_code">1</field>
</depends>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<web>
<url>
<hide_default_store_code>0</hide_default_store_code>
<redirect_to_url_without_store_code>301</redirect_to_url_without_store_code>
</url>
</web>
</default>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Noon_HideDefaultStoreCode" setup_version="1.0.4">
<module name="Noon_HideDefaultStoreCode" setup_version="1.0.5">
<sequence>
<module name="Magento_Store"/>
<module name="Magento_Backend"/>
Expand Down

0 comments on commit d7cf39e

Please sign in to comment.