How to add TLD to search exception in Safari
Asked Answered
S

3

7

How can I add .locahost to the list of TLDs that Safari will load — instead of searching — without supplying a protocol? Alternatively (or perhaps additionally), how do I get Safari to stop removing the http:// protocol from the URL?


I've been using project.localhost to handle local development for a while, now that .dev is no longer a viable development TLD. But I'm getting frustrated with Safari's default behavior. What I'd like to do is prevent Safari from submitting the domain name to the search engine.

Desired:

  1. enter project.localhost into address bar
  2. browser loads http://project.localhost

I would be satisfied with Safari not removing the http:// in the address bar, but I can't find a way to do that, either.

What actually happens:

  1. enter project.localhost into address bar
  2. browser sends project.localhost to Duck Duck Go
  3. user is tempted to test the ballistic properties of keyboard

Or, after I've loaded http://project.localhost and then try to add something after the current URL:

  1. click in address bar, URL has been shortened to project.localhost
  2. add /test.html to end
  3. browser sends project.localhost/test.html to Duck Duck Go
  4. user considers software violence
Semen answered 28/6, 2019 at 14:56 Comment(3)
This is clearly an extremely annoying Safari weirdness.Isolt
Indeed. If I could figure out installing Chromium – not Chrome – I might use it, instead. I've even tried installing Opera. When the installer fails, it doesn't build confidence.Semen
I'm currently pursuing development of a Safari extension that listens for the beforeSearch event. I can get it to abort the search when the query matching a pattern, but haven't yet figured out how to supply a replacement URL to Safari.Semen
S
2

Turns out that the Safari extension might be a long term solution. At this moment, I can't manage the developer membership cost. But here's the working code, for Future Me and anyone else looking into this.

In the extension's global page <script>:

(function() {
    safari.application.addEventListener("beforeSearch", function(event) {
        if (event.query.split("/")[0].split(".").pop() == "localhost") {
            event.preventDefault();
            safari.application.activeBrowserWindow.activeTab.url = "http://" + event.query;
        }
    }, true);
})();
Semen answered 30/7, 2019 at 21:42 Comment(2)
Thanks for sharing this! I know it's been a while, but I'm just encountering this problem. I'll be happy to publish a free tool for this if you can help me turn this into a real app. (I have not worked with Safari extensions before).Bellebelleek
@DannySung This is actually the only time I've tried my hand at a Safari extension. This many years later, and it's still essentially unsolved. It was easier to learn self-signed certificates and serve locally over HTTPS. Ironic.Semen
V
3

If you just type the trailing '/', then the search will be bypassed.

Valorie answered 15/4, 2021 at 12:36 Comment(0)
A
3

I just found this question while looking for a solution to the same problem. Good news, there's an easy solution that I just stumbled upon after not finding an answer.

Open System Preferences Open Network Click Advanced button Click Proxies tab

Then in the "Bypass proxy settings for these Hosts & Domains:" add in localhost to the list. Here's what I did with mine. I'm using .home for my local domains in my PiHole. Now if I type "router.home" into Safari my router's config page loads up.

screen shot of configured preferences

Afford answered 18/4, 2022 at 3:12 Comment(3)
And that works without even needing a check in the box?! I see, not even related to the "Select a protocol to configure" list. Must explore.Semen
Maybe I need to restart, or something. So far, this isn't working for me. I'm still excited that perhaps there's a fix and I'm just not doing it right.Semen
@MarkPriddy sorry, I hadn't seen your comment. I'm also running a PiHole and added a local DNS record for each of the boxes. So there might be something additional at play in my setup. They weren't working for me on first try which is when I found this post. After adding "*.home" like above "router.home" started working for me along with the others.Afford
S
2

Turns out that the Safari extension might be a long term solution. At this moment, I can't manage the developer membership cost. But here's the working code, for Future Me and anyone else looking into this.

In the extension's global page <script>:

(function() {
    safari.application.addEventListener("beforeSearch", function(event) {
        if (event.query.split("/")[0].split(".").pop() == "localhost") {
            event.preventDefault();
            safari.application.activeBrowserWindow.activeTab.url = "http://" + event.query;
        }
    }, true);
})();
Semen answered 30/7, 2019 at 21:42 Comment(2)
Thanks for sharing this! I know it's been a while, but I'm just encountering this problem. I'll be happy to publish a free tool for this if you can help me turn this into a real app. (I have not worked with Safari extensions before).Bellebelleek
@DannySung This is actually the only time I've tried my hand at a Safari extension. This many years later, and it's still essentially unsolved. It was easier to learn self-signed certificates and serve locally over HTTPS. Ironic.Semen

© 2022 - 2024 — McMap. All rights reserved.