Skip to content

'magento' adding in the URL when Use Web Server Rewrites set to NO using Console Command #25976

Open
@sandip5678

Description

@sandip5678

Summary (*)

This is a Developer experience issue. I have created one simple console file that will generate product name, URL etc.

  1. Magento 2.3.x with sample data
  2. Use Web Server Rewrites field set to No
  3. Clear cache if prompted

Examples (*)

  1. app/code/Custom/Pro/etc/di.xml
<?xml version="1.0"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Console\CommandListInterface">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="checkProductURL" xsi:type="object">Custom\Pro\Console\Command\ProductURLFetch</item>
            </argument>
        </arguments>
    </type>
</config>
  1. app/code/Custom/Pro/Console/Command/ProductURLFetch.php
<?php
 
namespace Custom\Pro\Console\Command;

use Magento\Catalog\Api\ProductRepositoryInterfaceFactory;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\ObjectManagerFactory;
use Magento\Framework\App\State;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


class ProductURLFetch extends \Symfony\Component\Console\Command\Command
{
    /**
     * @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface
     */
    protected $productAttributeRepository;

    /**
     * @var \Magento\Framework\Api\SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;

    /**
     * @var \Magento\Catalog\Model\ResourceModel\Attribute
     */
    protected $attributeResource;

    /**
     * @var State
     */
    protected $appState;

    protected $_productRepositoryFactory;

    /**
     * @var \Magento\Framework\EntityManager\EntityMetadata
     */
    protected $metadata;

    /**
     * Object manager factory
     *
     * @var ObjectManagerFactory
     */
    private $objectManagerFactory;
    /**
     * @var CollectionFactory
     */
    private $collectionFactory;

    /**
     * @param ObjectManagerFactory $objectManagerFactory
     * @param CollectionFactory $collectionFactory
     * @param State $appState
     * @param ProductRepositoryInterfaceFactory $productRepositoryFactory
     */
    public function __construct(
        ObjectManagerFactory $objectManagerFactory,
        CollectionFactory $collectionFactory,
        State $appState,
        ProductRepositoryInterfaceFactory $productRepositoryFactory
    ) {
        $this->objectManagerFactory = $objectManagerFactory;
        $this->appState = $appState;
        $this->collectionFactory = $collectionFactory;
        $this->_productRepositoryFactory = $productRepositoryFactory;
        parent::__construct();
    }

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this->setName('catalog:product:showurl');
        $this->setDescription('It will just show Product URLs.');
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
        
        $productCollection = $this->collectionFactory->create();
        $output->setDecorated(true);
        $productCollection->addAttributeToSelect('name','sku','url');        
        try {
            foreach ($productCollection as $product){
                $output->writeln( $product->getProductURL()  );
            }

            $output->writeln("<info>Product URLs has been generated..</info>");

            return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
        } catch (\Exception $exception) {

            $output->writeln("");
            $output->writeln("<error>{$exception->getMessage()}</error>");
            // we must have an exit code higher than zero to indicate something was wrong
            return \Magento\Framework\Console\Cli::RETURN_FAILURE;
        }
    }

}

Execute below console.
php bin/magento catalog:product:showurl

Output
URL in the output will be as follows:
http://www.example.com/magento/kir-1100.html

It should not contain 'magento' as in the URL.
http://www.example.com/kir-1100.html

Proposed solution

Looking into the file /vendor/magento/module-store/Model/Store.php
and function named _updatePathUseRewrites returns base URL with 'magento'.

Metadata

Metadata

Assignees

Labels

Component: CatalogUrlRewriteIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentPriority: P2A defect with this priority could have functionality issues which are not to expectations.Progress: PR in progressReproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchSeverity: S3Affects non-critical data or functionality and does not force users to employ a workaround.Triage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

Type

No type

Projects

Status

Pull Request In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions