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. 

Lets Configure the Plugin

In terms of setup this was very easy. Jump over to the settings inside BetterLinks

Update your settings, specifically these two: The link prefix, and enable custom domain. (because we are going to add our short custom domain)

Redirecting Our Custom Domain Inside Trellis.

The next thing we need to do is to tell trellis that we want to add a redirected domain to our site.
trellis/group_vars/production/wordpress_sites.yml
yml
				
					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)

trellis/roles/wordpress-setup/templates/wordpress-site.conf.j2
				
					{%- 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:

trellis/roles/wordpress-setup/templates/wordpress-site.conf.j2
				
					  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

				
			

Related Articles

The 4 C’s

Historically, I have been crippled by perfectionism, preventing me from sharing my journey and the lessons I’ve learned. I’ve embraced a new paradigm: “This is what I’m working on… Let me show you what I figured out.” This mindset shift allows me to share my experiences without judgment, inviting others to learn from my successes and failures. Join me as I explore the four levers of building an online business—Code, Content, Capital, and Collaboration—and discover how we can grow together in this entrepreneurial journey.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x