Is there a way to redirect certain URLs to specific web browsers in Linux?

I'm using Chrome as my default browser in Ubuntu 12.10. I need to use Firefox for business purposes (certain websites pertaining to my work only work with Firefox). Is there a way to force Ubuntu to use Firefox for certain types of URLs (maybe as defined by a regular expression) while maintaining Chrome as my default browser for all my other tasks? Perhaps as a shell script running in the background? I'd like this to work system-wide, covering links from Chrome itself as well as PDFs/ODTs, etc.

I have searched for solutions, but I couldn't find anything besides OpenWith, a Firefox extension which adds a button to open certain links in other browsers which would again require me to open Firefox beforehand, which does not help me at all.

Does anyone have any ideas? Something like Choosy for Linux?


The executable in Linux that opens a resource with your "favorite" program is called xdg-open. It is somewhat complicated, but you can add additional rules using xdg-mime install. However, I don't know if you can even do this for something like a specific URL.

This becomes further complicated in that specific desktop environments use different executables and methods for opening their default programs. For instance KDE has kde-open, but not every KDE application seems to use this.

Regardless, I got this partially working just using a shell script and a list of domains/URLs in a file. You can tweak it to your liking.

Note: You may need to repeat this process for other executables depending on your desktop environment. For example, Gnome has gvfs-open, KDE has kde-open, and XFCE has exo-open. (Even then, it might not work for every application.)

Note: The shell script is dependent on pcregrep being installed on your system.

  1. Find the xdg-open executable

    which xdg-open
    
  2. Create a new directory in your home folder

    mkdir -p ~/.local/bin
    
  3. Add the following to your ~/.bashrc file

    ~/.bashrc

    export PATH="${HOME}/.local/bin:${PATH}"
    
  4. Create the shell script in the newly created folder. (Be sure to edit the top four variables so they are correct for your system!)

    ~/.local/bin/xdg-open

    #!/bin/bash
    
    DOMAIN_LIST_FILE=~/'domains.txt'
    OTHER_BROWSER='/path/to/other-browser' # For instance /usr/bin/firefox
    BROWSER_OPTIONS='' # Optional, for command line options passed to browser
    XDG_OPEN='/path/to/xdg-open'
    
    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
    fi
    
    "$XDG_OPEN" ${*}
    
  5. Create the list of domains in your home folder

    ~/domains.txt

    stackexchange.com
    stackoverflow.com
    superuser.com
    
  6. Log out and log back in to have the settings take effect