2014-10-13 09:49:30 +02:00
title: Disable Anchor Rewriting
# Disable Anchor Rewriting
Anchor links are links with a "#" in them. A frequent use-case is to use anchor links to point to different sections of
the current page. For example, we might have this in our template:
2017-08-07 05:11:17 +02:00
```ss
2017-10-27 04:38:27 +02:00
< ul >
< li > < a href = "#section1" > Section 1< / a > < / li >
< li > < a href = "#section2" > Section 2< / a > < / li >
< / ul >
2017-08-07 05:11:17 +02:00
```
2014-10-13 09:49:30 +02:00
Things get tricky because of we have set our `<base>` tag to point to the root of the site. So, when you click the
first link you will be sent to http://yoursite.com/#section1 instead of http://yoursite.com/my-long-page/#section1
In order to prevent this situation, the SSViewer template renderer will automatically rewrite any anchor link that
doesn't specify a URL before the anchor, prefixing the URL of the current page. For our example above, the following
would be created in the final HTML
2017-08-07 05:11:17 +02:00
```ss
2017-10-27 04:38:27 +02:00
< ul >
< li > < a href = "my-long-page/#section1" > Section 1< / a > < / li >
< li > < a href = "my-long-page/#section2" > Section 2< / a > < / li >
< / ul >
2017-08-07 05:11:17 +02:00
```
2014-10-13 09:49:30 +02:00
There are cases where this can be unhelpful. HTML anchors created from Ajax responses are the most common. In these
situations, you can disable anchor link rewriting by setting the `SSViewer.rewrite_hash_links` configuration value to
`false` .
2018-06-25 00:39:53 +02:00
**app/_config/app.yml**
2017-08-07 05:11:17 +02:00
```yml
2017-10-26 02:22:02 +02:00
SilverStripe\View\SSViewer:
2014-10-13 09:49:30 +02:00
rewrite_hash_links: false
2017-08-07 05:11:17 +02:00
```
2014-10-13 09:49:30 +02:00
2019-04-16 22:22:27 +02:00
Alternatively, it's possible to disable anchor link rewriting for specific pages using the `SSViewer::setRewriteHashLinksDefault()` method in the page controller:
2014-10-13 09:49:30 +02:00
2017-08-07 05:11:17 +02:00
```php
2019-04-16 22:22:27 +02:00
namespace Example\HashLink;
use PageController;
2017-10-27 04:38:27 +02:00
use SilverStripe\View\SSViewer;
2019-04-16 22:22:27 +02:00
class ExamplePageController extends PageController
2017-10-27 04:38:27 +02:00
{
2019-04-16 23:32:55 +02:00
protected function init()
{
2019-04-16 22:22:27 +02:00
parent::init();
SSViewer::setRewriteHashLinksDefault(false);
}
2017-10-27 04:38:27 +02:00
}
```