Skip to content

Commit d7cf39e

Browse files
committed
Fix 'Status code is 302 instead of 301 #10'
1 parent e19b448 commit d7cf39e

File tree

8 files changed

+57
-18
lines changed

8 files changed

+57
-18
lines changed

Helper/Data.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,44 @@
55
class Data extends \Magento\Framework\App\Helper\AbstractHelper
66
{
77
const XML_PATH_HIDE_DEFAULT_STORE_CODE = 'web/url/hide_default_store_code';
8+
const XML_PATH_REDIRECT_TO_URL_WITHOUT_STORE_CODE = 'web/url/redirect_to_url_without_store_code';
9+
10+
const NO_REDIRECT = 0;
11+
const PERMANENT_REDIRECT = 301;
12+
const TEMPORARY_REDIRECT = 302;
813

914
/**
10-
*
1115
* @return boolean
1216
*/
1317
public function isHideDefaultStoreCode()
1418
{
15-
if ($this->scopeConfig->getValue(self::XML_PATH_HIDE_DEFAULT_STORE_CODE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
19+
if ($this->scopeConfig->getValue(
20+
self::XML_PATH_HIDE_DEFAULT_STORE_CODE,
21+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
22+
)) {
1623
return true;
1724
}
1825
return false;
1926
}
27+
28+
/**
29+
* @return int
30+
*/
31+
public function getRedirectCode()
32+
{
33+
$code = $this->scopeConfig->getValue(
34+
self::XML_PATH_REDIRECT_TO_URL_WITHOUT_STORE_CODE,
35+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
36+
);
37+
38+
if ($code == 301) {
39+
return self::PERMANENT_REDIRECT;
40+
}
41+
42+
if ($code == 1) {
43+
return self::TEMPORARY_REDIRECT;
44+
}
45+
46+
return self::NO_REDIRECT;
47+
}
2048
}

Observer/RedirectWithoutStoreCode.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44

55
class RedirectWithoutStoreCode implements \Magento\Framework\Event\ObserverInterface
66
{
7-
/**
8-
* @var \Magento\Framework\App\Response\RedirectInterface
9-
*/
10-
protected $redirect;
11-
127
/**
138
* @var \Magento\Framework\App\ActionFlag
149
*/
@@ -31,20 +26,17 @@ class RedirectWithoutStoreCode implements \Magento\Framework\Event\ObserverInter
3126

3227
/**
3328
*
34-
* @param \Magento\Framework\App\Response\RedirectInterface $redirect
3529
* @param \Magento\Framework\App\ActionFlag $actionFlag
3630
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
3731
* @param \Noon\HideDefaultStoreCode\Helper\Data $helper
3832
* @param \Magento\Framework\UrlInterface $url
3933
*/
4034
public function __construct(
41-
\Magento\Framework\App\Response\RedirectInterface $redirect,
4235
\Magento\Framework\App\ActionFlag $actionFlag,
4336
\Magento\Store\Model\StoreManagerInterface $storeManager,
4437
\Noon\HideDefaultStoreCode\Helper\Data $helper,
4538
\Magento\Framework\UrlInterface $url
4639
) {
47-
$this->redirect = $redirect;
4840
$this->actionFlag = $actionFlag;
4941
$this->storeManager = $storeManager;
5042
$this->helper = $helper;
@@ -65,11 +57,15 @@ public function execute(\Magento\Framework\Event\Observer $observer)
6557
$url = $this->url->getCurrentUrl();
6658
$pos = strpos($url, $this->storeManager->getStore()->getBaseUrl() . $defaultStore->getCode());
6759

68-
if ($this->helper->isHideDefaultStoreCode() && $pos !== false) {
60+
if (
61+
$this->helper->isHideDefaultStoreCode() &&
62+
$pos !== false &&
63+
$code = $this->helper->getRedirectCode()
64+
) {
6965
$controller = $observer->getData('controller_action');
7066
$url = str_replace('/' . $defaultStore->getCode() . '/', '/', $url);
7167
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
72-
$this->redirect->redirect($controller->getResponse(), $url);
68+
$controller->getResponse()->setRedirect($url, $code);
7369
}
7470
}
7571
}

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# HideDefaultStoreCode
22

3-
Magento 2 plugin for hide *Default Store Code* from URL.
3+
Magento 2 module for hide *Default Store Code* from URL.
44

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

7-
![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)
7+
![howto-hide-default-store-code-from-url-magento-2](doc/img/hide-default-store-code-from-url-magento-2-1-1.png)
88

99
***
1010

11-
## Installation
11+
## INSTALLATION
1212

1313
### manual
1414

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

31-
## Configuration
31+
## CONFIGURATION
32+
33+
### ENABLE/DISABLE
3234

3335
*Stores > Configuration > General > Web > Url Options > Hide Default Store Code*
36+
37+
### REDIRECT
38+
39+
*Stores > Configuration > General > Web > Url Options > Auto-redirect to URL without Store Code*

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"magento/module-backend": ">=100.0"
1212
},
1313
"type": "magento2-module",
14-
"version": "1.0.4",
14+
"version": "1.0.5",
1515
"autoload": {
1616
"files": [
1717
"registration.php"
Loading

etc/adminhtml/system.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
<label>Hide Default Store Code</label>
88
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
99
</field>
10+
<field id="redirect_to_url_without_store_code" translate="label comment" type="select" sortOrder="101" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
11+
<label>Auto-redirect to URL without Store Code</label>
12+
<source_model>Magento\Config\Model\Config\Source\Web\Redirect</source_model>
13+
<comment>I.e. redirect from http://example.com/store/ to http://example.com/</comment>
14+
<depends>
15+
<field id="web/url/hide_default_store_code">1</field>
16+
</depends>
17+
</field>
1018
</group>
1119
</section>
1220
</system>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<web>
55
<url>
66
<hide_default_store_code>0</hide_default_store_code>
7+
<redirect_to_url_without_store_code>301</redirect_to_url_without_store_code>
78
</url>
89
</web>
910
</default>

etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="Noon_HideDefaultStoreCode" setup_version="1.0.4">
3+
<module name="Noon_HideDefaultStoreCode" setup_version="1.0.5">
44
<sequence>
55
<module name="Magento_Store"/>
66
<module name="Magento_Backend"/>

0 commit comments

Comments
 (0)