How to Open Certain URL Links in Specific Web Browsers

Last updated: October 15, 2024 — Leave a comment

This tutorial shows how to configure Ubuntu or other Linux to redirect certain URLs or domains to specific web browser, while leaving all others open in the default browser.

When clicking an URL in email reader, chat app, and other apps, it by default opens the linked page in system default web browser. However, some users may prefer to open certain websites in non-default browser. For example, use Google Chrome for watching YouTube, while using Firefox as default.

Besides using a third-party app (e.g., Junction) to pop-up dialog letting you choose which app to open the link with, Linux user can create a simple script to tell which browser to use when clicking an URL.

Junction lets you choose which app to open link with

And, below I’m going to show to how to do the trick step by step.

NOTE: The steps below are written for default GNOME Desktop. It ALSO works in other desktops, though you may need to change the way to create the files and configure default browser.

Step 1: Find out path to executable files of your web browsers

For beginners who don’t even know where are the executable files of the web browsers in system, first press Ctrl+Alt+T on keyboard to open up a terminal window.

To find the browsers installed as native .deb (or .rpm) packages, try the command below:

whereis firefox

Replace firefox with yours web browser name. NOTE: Linux console in case sensitive, so you may also try Firefox if the last command output nothing.

Native app packages usually install executable files into /usr/bin. So, you may also run ls /usr/bin to list all files in that directory.

If your browsers were installed via Snap and/or Flatpak packages, then run the commands below to check the following bin directories:

ls /snap/bin
ls /var/lib/flatpak/exports/bin/

So, path to executable of your web browser, firefox for example, should be similar to one of below:

  • /usr/bin/firefox
  • /snap/bin/firefox
  • /var/lib/flatpak/exports/bin/org.mozilla.firefox

Step 2: Create custom script to tell which browser to use when clicking an URL

1. First, search for and launch Text Editor from the overview screen.

2. When text editor opens with an empty page (create if not), paste following script content:

#!/bin/bash
DOMAIN_LIST_FILE=~/'.domains.txt'
OTHER_BROWSER='/usr/bin/chromium'
BROWSER_OPTIONS='' # Optional, for command line options passed to browser
DEFAULT_BROWSER='/usr/bin/firefox'

if echo "$1" | pcregrep -q '^https?://'; then
    matching=0
    while read domain; do
	if echo "$1" | pcregrep -q "^https?://${domain}"; then
	    matching=1
	    break
	fi
    done < "$DOMAIN_LIST_FILE"

    if [[ $matching -eq 1 ]]; then
	"$OTHER_BROWSER" $BROWSER_OPTIONS ${*}
	exit 0
    fi

    "$DEFAULT_BROWSER" ${*}
    exit 0
else
    exit 0
fi

The script above (inspired by this Wiki) tells to check the .domains.txt file for certain URLs. If clicking an URL matches one in that file, it tries to open with OTHER_BROWSER, or it uses the DEFAULT_BROWSER instead.

Here, you need to replace /usr/bin/xxx with PATH to your web browsers’ executable:

  • OTHER_BROWSER=’/usr/bin/chromium‘, according to which browser you want to use for certain URLs.
  • DEFAULT_BROWSER=’/usr/bin/firefox‘ for all other URLs.
  • And add optional options, if want, to BROWSER_OPTIONS='', such as --new-window to open in new window.

3. When done editing the file, click Save (Ctrl+S or Ctrl+Alt+S). In pop-up dialog, do:

  • set script name, open-custom-url in my case.
  • choose save the script to /usr/local/bin(need to type password to authenticate), or any other location (e.g., Home -> .local (press Ctrl+H to view/hide) -> bin) if you don’t have admin permission.

Finally, open terminal (Ctrl+Alt+T) and run command to add executable permission to the script:

sudo chmod +x /usr/local/bin/open-custom-url

Replace /usr/local/bin/open-custom-url if you saved the file to other location, and skip sudo for file inside user home.

Step 3: Create Desktop Entry for that script

1. Also open text editor and create an empty document. Then, paste the lines below into that file:

[Desktop Entry]
Version=1.0
Name=Custom Web
GenericName=Web Browser
Exec=/usr/local/bin/open-custom-url %u
Terminal=false
NoDisplay=true
Type=Application
MimeType=text/html;text/xml;application/xhtml+xml;application/vnd.mozilla.xul+xml;text/mml;x-scheme-handler/http;x-scheme-handler/https;
StartupNotify=true
Categories=Network;WebBrowser;
Keywords=web;browser;internet;
Actions=new-window;new-private-window;

Here you need to replace /usr/local/bin/open-custom-url to path to the script you created via Step 2.

2. Then, click Save (Ctrl+S) and choose to save file:

  • as whatever name with .desktop extension.
  • save to Home -> .local -> share -> applications directory.

After that, your Linux system knows the script, creates an entry in the start menu (but hidden due to NoDisplay=true), and allows to be associated with other apps/files.

Step 4: Create .domains.txt file and write the certain URLs

Now, open text editor and write the certain URLs or domains you want to open with the specific web browser, one URL per line.

When done, choose save as .domains.txt (according to script content in Step 2) file in user home directory.

Step 5: Set the custom script as default ‘browser’

After created the script, added as desktop entry, and wrote certain URLs into file via steps above, you also need to run few commands below:

  • First, open terminal (Ctrl+Alt+T) and run command to install pcregrep tool which is in use in the script:
    sudo apt install pcregrep
  • Then, add executable permission to that script:
    sudo chmod +x /usr/local/bin/open-custom-url
  • Finally, update the database for the desktop entry:
    update-desktop-database $HOME/.local/share/applications/

When done, open Settings. Then navigate to either Apps or Default Applications in left panel.

Finally, go to Default Apps -> choose “Custom Web” for Web. For non-GNOME desktop, use the corresponding setting option to set the custom script as default browser.

Finally, try opening an URL link in your email client or other non-web-browser applications to see if works.

Undo:

To undo the changes above, simply delete the script and .desktop files you created. If you use the names in the steps above, just run commands below in terminal (Ctrl+Alt+T) one by one:

sudo rm /usr/local/bin/open-custom-url
rm ~/.local/share/applications/open-custom-url.desktop

Also open “Settings” and set back the default web browser.

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via [email protected] Buy me a coffee: https://ko-fi.com/ubuntuhandbook1 |

No Comments

Be the first to start the conversation.

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*