TOPIC: NUMERAL SYSTEMS
WordPress URL management with canonical tags and permalink simplification
29th March 2025Recently, I have been going through the content, rewriting things where necessary. In the early days, there were some posts following diary and announcement styles that I now avoid. Some now have been moved to a more appropriate place for those, while others have been removed.
While this piece might fall into the announcement category, I am going to mix up things too. After some prevarication, I have removed dates from the addresses of entries like this after seeing some duplication. Defining canonical URL's in the page header like this does help:
<link rel="canonical" href="https://technologytales.com/an-alternate-approach-to-setting-up-a-local-git-repository-with-a-remote-github-connection/">
However, it becomes tricky when you have zero-filled and non-zero-filled dates going into URL's. Using the following in a .htaccess
file redirects the latter to the former, which is a workaround:
RewriteRule ^([0-9]{4})/([1-9])/([0-9]{1,2})/(.*)$ /$1/0$2/$3/$4 [R=301,L]
RewriteRule ^([0-9]{4})/(0[1-9]|1[0-2])/([1-9])/(.*)$ /$1/$2/0$3/$4 [R=301,L]
The first of these lines zero-fills the month component, while the second zero-fills the day component. Here, [0-9]{4}
looks for a four digit year. Then, [1-9]
picks up the non-zero-filled components that need zero-prefixing. The replacements are 0$2
or 0$3
as needed.
Naturally, this needs URL rewriting to be turned on for it to work, which it does. Since my set-up is on Apache, the MOD_REWRITE module needs to be activated too. Then, your configuration needs to allow its operation. With dates removed from WordPress permalinks, I needed to add the following line to redirect old addresses to new ones for the sake of search engine optimisation:
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ /$4
Here, [0-9]{4}
picks up the four digit year and [0-9]{2}
finds the two-digit month and day. The, (.*)
is the rest of the URL that is retained as signalled by /$4
at the end. That redirects things nicely, without my having to have a line for every post on the website. Another refinement was to remove query strings from every page a visitor would see:
RewriteCond %{REQUEST_URI} !(^/wp-admin/|^/wp-login\.php$) [NC]
RewriteCond %{QUERY_STRING} .
RewriteCond %{QUERY_STRING} !(&preview=true) [NC]
RewriteRule ^(.*)$ /$1? [R=301,L]
This still allows the back end and login screens to work as before, along with post previews during the writing stage. One final note is that I am not using the default login address for the sake of added security, yet that needs to be mentioned nowhere in the .htaccess
file anyway.
Octals in UNIX shell scripting
9th February 2007I have just discovered that if you have a number with a leading zero, such as 08
, it is assumed to be an octal number, that is, one of base 8. The upshot of this is that you get errors when you have numbers like 08
and 09
in your arithmetical expressions; they are illegal in octal: 08
should be 10
and 09
should be 11
. Of course, as luck would have it, you get exactly these expressions when date/time processing. Luckily, you can force things to be base 10 by having something like 10#08
or, when extracting the minute from a date-time value, 10#$(date +%M)
. Strange as it might appear, this behaviour is all by design. It is dictated in the POSIX standard that governs UNIX. That said, I'd rather it if 08 was interpreted as an 8 and 09 as a 9 rather than triggering the errors that we see, but that could have been seen as muddying the simplicity of the standard.