URL ReWrite & Relative URIs

Have you noticed how the permalink urls of wordpress contain the date as yyyy/mm/dd/post-title? And if you just type https://poeticcode.wordpress.com/ you get all the posts in that year and if you use // then all the posts within that month? This can be done using what’s called URL Rewriting. So, a URL such as

https://poeticcode.wordpress.com/2007/12/ gets rewritten to something like

https://poeticcode.wordpress.com/posts.do?year=2007&month=12 (this is not the exact link that wordpress uses).

This is usually a good approach, especially since it’s considered as a SEO friendly format. However, with this approach, one issue is that the generated HTML assumes the base of the document to be all the way up to /2007/12, when in reality, your base is actually at /. So, this can be easily fixed by using the BASE tag of the HTML.

If you are like me, who maintains a separate test site and a production site, then it gets a bit tricky. This is because of two reasons. One is that the name of the test and production servers is different. Second, the test server may not be setup to be starting from the root directory (/). For the first problem, it may be tempted to write the base tag with href pointing to just ‘/’. However, as per the BASE tag reference at w3.org, “This attribute specifies an absolute URI that acts as the base URI for resolving relative URIs.” That is, the base tag’s href has to be absolute.

So, the following perl code help to fix up this base tag.

$server = $ENV{SERVER_NAME};
$script = $ENV{SCRIPT_NAME};
$script =~ s/[^\/]+$//; # knock off the name and retain the directory
$base = "http://$server$script";

then, you can out put

Here, there is no need to use the $script if the test setup is also at the root directory level. Then, it would just be

$base = “http://$server/”;

I am sure this technique can be extended to take care of the http scheme (http vs https) and the non-default port.

Advertisements

Leave a comment

Filed under URL Rewrite

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s