Ben Lobaugh Online

I think, therefore I am. I am, therefore I sail

Category: Web Development Page 1 of 30

Looping stairs

How to fix a WordPress HTTPS redirect loop with an NGINX reverse proxy

If your WordPress site is set up to use HTTPS and a reverse proxy, such as an NGINX reverse proxy, is put in front of it you may wind up with an infinite redirect loop.

Following the redirect in dev tools, it looks like this is happening:
https://example.com -> https://example.com

A head scratcher for sure, but understanding what is going on behind the scenes reveals the issue and the solution together.

Here is what is actually happening:

  • Request is made to https://example.com
  • The reverse proxy catches the request and makes it’s own request to http://example.com. Take special note that the schema changed to http.
  • The WordPress site sees a request for http://example.com and says, “Hey, that’s not right, I am at https://example.com” and tells the browser to go there
  • Repeat indefinitely

You could change the site to support http to the exclusion of https, however that is hacky and anything wanting https will still work itself into an infinite redirect.

An easier solution is to trick WordPress into thinking the request is https enabled.

WordPress looks at a server variable when determining the status of https. Open your wp-config.php file and add the following just after the <?php tag:

if ( $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' ) {
    $_SERVER['HTTPS'] = 'on';
    $_SERVER['SERVER_PORT'] = 443;
}

And now your site will work as originally anticipated.

Dastardly isn’t it 😉

Photo by Dan Freeman on Unsplash

WordPress: How to “properly” allow unfiltered uploads

If you are working with a WordPress site and getting the dreaded “Sorry, this file type is not permitted for security reasons.” message, fret no more!

WordPress has a config that allows you to enable unfiltered uploads

define( 'ALLOW_UNFILTERED_UPLOADS', true );

This, however, does not always work. Especially on Multisite, which may only allow unfiltered uploads for super admin users. No bueno.

Filters to the rescue!

Drop this code into your project (I put it in mu-plugins) and viola! You will have unfettered uploads.

add_filter( 'user_has_cap', 'unfiltered_upload' );

function unfiltered_upload( $caps )
{
    $caps['unfiltered_upload'] = 1;
    return $caps;
}

See also this StackExchange message

Cookies disappear after adding Set-Cookies in .htaccess? Here is how to get them back

I needed to set a cookie via .htaccess, to ensure it was always in the browser, regardless of what the website was doing. Setting a cookie is rather easy, it will look similar to:

Header set Set-Cookie "cookieName=value; Expires=Wed, 15 May 2222 07:28:00 GMT"

The next morning I attempted to login to the site and could not. It turned out the login cookies were not being set. The issue was insidious. Re-read the line above, at first glance it seems fine when you want to set a cookie, however if there are other cookies that need to run you need to add the cookie.

This three letter change resolved the issue and allows all the other cookies to operate properly:

Header add Set-Cookie "siglock=hello-world; Expires=Wed, 15 May 2222 07:28:00 GMT"

Canonical URL plugin for WordPress released!

Have you run into this scenario? You have content that needs to live on multiple sites but you are concerned about SEO issues duplicating said content?

As search engines crawl the web looking for pages to include in their search indexes they may run across content duplicated from another site. When that happens the search engine will first attempt to determine which site is the “source of truth”. The site with duplicated content will be penalized in rankings. If no source can be found both sites rankings will be penalized. An SEO nightmare.

Is it possible to distribute the same content to multiple sites without incurring this penalty? The answer is a resounding YES! All you need to do is let the search engine know where the real of the article is, the canonical article. This is done by including a tag in the html head area. It is for search engines, humans do not typically see it.

WordPress has some built in canonical abilities however in WordPress itself there is no way to set the canonical url. I tend to have articles published all over the web that I would like to copy to this site for posterity. Today I whipped up a little plugin that provides a simple url field to add the article’s canonical url in a search engine friendly way.

It is as simple as adding the url as you create the article!

Publish your post and WordPress will handle the rest.

Here is an example post that I wrote on team building for WebDevStudios.

9 Critical Concepts for Leading High-Performance Teams

You may notice there is also a brief disclaimer at the top of the article. That is optional and may be helpful to some readers and/or required by the site you are reposting from to comply with guidelines.

If you want to use this plugin on your own site simply head over to Github and download the plugin!

https://github.com/blobaugh/canonical-url-for-wordpress

Find total number of post records on a WordPress Multisite in MySQL

I was recently asked for the total number of posts on a WordPress website. Normally as simple as looking at the number in the dashboard, however in this instance the WordPress install was a Multisite with nearly 100 sites. That would have taken far too long to do manually, but what about having MySQL do the heavy lifting for me?

The information_schema database contains data about every database and table on the server. One of the metrics it stores is the count of rows in a table. We can use that to extrapolate roughly how many entries are in all the posts tables combined. This is not an exact science, however it will get you close to the real numbers. I argue that it will typically be “close enough” on larger installs, such as the one I was looking at.

In the following query, just replace the database name with your database and run it.

SELECT SUM( table_rows )
FROM information_schema.TABLES 
WHERE 
	table_schema = "{{YOUR_DATABASE_NAME}}" 
	AND table_name LIKE '%_posts';

Page 1 of 30