Installing Better Links By WPDeveloper

What's The Problem??
I want to use BetterLinks By WPDeveloper to manage the Affiliate Links on the website and in my YouTube videos. But because when we use Trellis and Bedrock the wordpress install gets put into a subdirectory of the wordpress root. For most things, this is no issue. It just means that the link address will look like this:
https://yourdigitaltoolbox.com/wp/go/The-Actual-Link-Slug
This is already WAY too long for linking. I purchased a new domain just for links:
https://ydtb.link/
This is much shorter. Let’s figure out what it takes to make this work with Roots Trellis.Â
Lets Install BetterLinks by WPDeveloper.
The installation of this is mostly the same as installing any plugin in the wordpress environment. It is likely that you log in to the WordPress back end, upload the plugin to the plugins page, and then activate the plugin.Â
I like to be a little weird and so I use PHP Composer with my own private repository of paid plugins. So installing any plugin for me looks like this:Â
trellis vm shell #to get into the VM thats locally running our site
composer require wpdeveloper/betterlinks wpdeveloper/betterlinks-pro
Then we commit our changes to the website by committing our changes to the site with:
git add *
git commit -m "Added WPDeveloper Plugin to the site"
git push
I have a quick Shell script that basically does the same thing but makes for less typing. So instead of doing all three of those commands, you could also do:Â
trellis update "Added WPDeveloper Plugin to the site"
The message is optional. If you don’t provide one it defaults to “Updated Website” or some such.
That shortcut uses trellis-cli plugin functionality to add additional (non-standard) scripts to your trellis CLI.
I’m working on another video that explains how to add functionality to your Trellis-CLI.
If you are interested, leave a comment below with what you want me to cover.Â
Redirecting Our Custom Domain Inside Trellis.
wordpress_sites:
yourdigitaltoolbox.com:
... OTHER VALUES HERE ...
site_hosts:
- canonical: yourdigitaltoolbox.com
redirects:
- www.yourdigitaltoolbox.com
- ydtb.link
... MORE VALUES ...
Adding this redirect (line 8) says that any traffic coming to ydtb.link should be redirected to yourdigitaltoolbox.com ( Substitute your domains respectively ). If you have a www subdomain, it will also be here.Â
We need to make one more change to Trellis to make this seamless. The BetterLinks Custom Domain Server Configuration section says to do this in the following picture, but we will do something different. So don’t Follow this:
Inside the wordpress-site.conf.j2 file (the one that has our server configuration), we are going to first look at what is there by default, and then we are going to add a section. ( This section starts on line 288 in the actual file ) This is how the file is before we modify it. We are really only interested in updating lines 23-25 (310-313 in the actual file)
{%- block redirects_domains %}
{% if site_hosts_redirects | default([]) | count %}
# Redirect some domains
{% endif %}
{% for host in item.value.site_hosts if host.redirects | default([]) %}
server {
{% if ssl_enabled -%}
listen [::]:443 ssl;
listen 443 ssl;
{% endif -%}
listen [::]:80;
listen 80;
http2 {{ nginx_http2_enabled | default(false) | ternary('on', 'off') }};
http3 {{ nginx_http3_enabled | default(false) | ternary('on', 'off') }};
server_name {{ host.redirects | join(' ') }};
{{ self.https() -}}
{{ self.acme_challenge() -}}
{{ self.includes_d() -}}
location / {
return 301 {{ ssl_enabled | ternary('https', 'http') }}://{{ host.canonical }}$request_uri;
}
}
{% endfor %}
{% endblock %}
Here’s the change we need to make:
location / {
if ($host = "ydtb.link") {
if ($request_uri !~* "^/go") {
return 301 {{ ssl_enabled | ternary('https', 'http') }}://{{ host.canonical }}/wp/go$request_uri;
}
return 301 {{ ssl_enabled | ternary('https', 'http') }}://{{ host.canonical }}/wp$request_uri;
}
return 301 {{ ssl_enabled | ternary('https', 'http') }}://{{ host.canonical }}$request_uri;
}
If the host is <ydtb.link> then do some custom stuff.
Otherwise, do what you were doing before. Change the hard-coded domain to your short link domain.Â
If the request_uri is missing the ‘/go’, let’s add it to our redirect URLÂ
Otherwise, just add the ‘/wp’ and return that.Â
Note: Yes, I understand that we are hard-coding this value, but this is not getting patched back to the Roots Trellis Core,
so it’s fine… okay?Â
Re-Provision Trellis
Ok, let’s deploy our changes to our staging site, or live dangerously and just push these changes to the production site. 🤪Â
git add *
git commit -m "Added BetterLinks Redirect Config"
git push
--- OR ---
trellis update "Added Better Links Redirect Config"
Before we run the Re-Provision command we need to make sure that our new short link domain DNS Settings are setup correctly.Â
We just need to add a CNAME for ydtb.link -> yourdigitaltoolbox.com
This is so that when trellis tries to create an SSL certificate for ydtb.link it goes to the right place
(And so that when people use the domain it goes to the right place )Â
Â
trellis provision staging
--- OR ---
trellis provision production
This is going to do all the work of making the changes we just added.Â
Final Configuration & Results
Home stretch now.Â
Back in the custom domain area we are going to add the domain and click verify. It should think for a second and then give you a ‘domain verified’ checkmark and then let you save the settings below with the Save Settings
button.Â
We can now create some test links and try them out!Â
Because we have the rule setup correctly then you can use either of these and they will go to the same place.Â
https://ydtb.link/go/betterlinks -> successful
https://ydtb.link/betterlinks -> successful