1

I am creating a shop with only one product. I want this product page to be the homepage, or instantly redirect to that product page when i visit the store. Any idea how?

I did find this option to set a homepage url:

enter image description here

But if i simply enter

/product-sku.html

there then it won't work and my homepage returns a 404.

3 Answers 3

1

I ended up creating a module and using the following code:

etc/frontend/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
    <observer name="controller_action_predispatch" instance="BB\BB\Observer\ControllerPredispatch" />
</event>
</config>

Observer/ControllerPredispatch.php

<?php

namespace BB\BB\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class ControllerPredispatch implements ObserverInterface
{
    /**
     * @var \Magento\Framework\UrlInterface
     */
    protected $url;

    /**
     * @var \Magento\Framework\App\Response\Http
     */
    protected $http;

    /** @var \Magento\Customer\Model\Session */
    protected $customerSession;

    /**
     * @param \Magento\Framework\UrlInterface $url
     * @param \Magento\Framework\App\Response\Http $http
     * @param \Magento\Customer\Model\Session $customerSession
     */
    public function __construct(
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\Response\Http $http,
        \Magento\Customer\Model\Session $customerSession
    )
    {
        $this->url = $url;
        $this->http = $http;
        $this->customerSession = $customerSession;
    }

    /**
     * Manages redirect
     */
    public function execute(Observer $observer)
    {
        /**
         * Check if user logged in
         */
        if ($this->customerSession->isLoggedIn()) {
            return;
        }

        if ($observer->getRequest()->getFullActionName() == 'cms_index_index') {
            /**
             * Redirect to login
             */
            $this->http->setRedirect($this->url->getUrl('bb-check-extension.html'), 301);
        }
    }
}

Code is created by SR: Magento 2 : Redirect From Homepage to Login if not Logged

0

There is actually an easier way, in the default web url put:

catalog/product/view/id/1

Where "1" is the id of your product you want to show. I have tested it and it's working.

0

the second answer with catalog/product/view/id/1 will break your install both the front-end and the backend and you won't be able to change the value back

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.