简体   繁体   中英

Magento: how to include anchor link in URL rewrite

I'm very new to coding and hope someone can help me.

On Magento Commerce 1.7 I'm running 3 stores (3 languages). I have added an anchor link (#example) to point to a certain position on our policies (CMS) page, which is available in all three stores in their respective languages. However, each policies (CMS) page has it's own URL in it's language. To switch from one store to the other I've had to use the URL rewrite in Magento so as not to get a 404.

English site: www.example.com/en/policies/#example
German site: www.example.com/de/agb_s/#example
Dutch site: www.example.com/nl/algemene_voorwaarden/#example

However, the URL rewrite doesn't accept the #anchor symbol and just adding it at the end of the URL in the php code means it just disappears as soon as the rewrite kicks in.

    <?php echo $this->getUrl('policies/') ?>#example"

Is there a way to write the anchor into the php or the URL rewrite so that it doesn't get lost?

Thank you in advance.

OK, found it.

In the URL rewrite in Magento backend the "#" anchor symbol in the request path is not allowed but the "#" anchor symbol is allowed in the target path. So, in the phtml file where you have the link, instead of this code:

<a href="<?php echo $this->getUrl('policies')?>#example" >example</a>

change the code to:

<a href="<?php echo $this->getUrl('policies/example')?>" >example</a>

and in Magento redirect this URL to:

  • English store: policies/example -> policies/#example
  • German store: policies/example -> agb_s/#example
  • Dutch store: policies/example -> algemene_voorwaarden/#example

The URL "policies/example" doesn't exist but that doesn't matter. The URL rewrite will redirect it to your anchor on the store's respective policies page. Might not be pretty but it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM