I want to get URL which is passed in the address bar but problem is that in my .htaccess there is a code for the rewrite I don't know for that reason or something else I got other URL which is not matched with my browser URL

Browser URL is https://www.example.com/category/112-model+name-azx+xza.html

And with this code "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"

I got http://www.example.com/azx-xza-asd.html" Which is stored in product detail

I want https://www.example.com/category/112-model+name-azx+xza.html

  • you want to this type of url - www.example.com/category/product.html Feb 19 '19 at 5:13
  • No, I mentioned in question I want URL which I SHow in my address bar. Feb 19 '19 at 5:14
  • www.example.com/product.html Feb 19 '19 at 5:16
  • Can you please provide the htaccess code which does your rewrite, especially if it is not magento standard?
    – HelgeB
    Feb 19 '19 at 5:24
  • RewriteCond %{REQUEST_URI} ^/category/.*$ RewriteRule ^category/([0-9]+)-([a-zA-Z0-9+]+)-([a-zA-Z0-9+()']+)-([a-zA-Z0-9+]+)-asd\.html$ /category/index.php?category=category+name&year=$1&make=$2&model=$3&battery=$4 [L] Feb 19 '19 at 5:27

You can get the current url with the following code:

$url = \Magento\Framework\App\ObjectManager::getInstance() ->get('Magento\Framework\UrlInterface');
  • Sorry @HelgeB but with this code, I get the same result which I don't want. Feb 19 '19 at 5:19

Another way:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');

To get current URL without Query String, add below line:

$currenturl = substr($currenturl, 0, strpos($currenturl, "?"));
  • Sorry got the same Result, not get address bar URL. Feb 19 '19 at 5:25

Can you try this ?

$request = \Magento\Framework\App\ObjectManager::getInstance() ->get('\Magento\Framework\App\RequestInterface');

echo $request->getUriString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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