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.



Filed under URL Rewrite

7 responses to “URL ReWrite & Relative URIs

  1. Thanks for another magnificent article. The place else may anybody get that kind of
    information in such a perfect way of writing?
    I’ve a presentation next week, and I am on the look
    for such info. http://obattumorlambung07.wordpress.com/

  2. you are actually a excellent webmaster. The website loading speed is amazing.
    It sort of feels that you’re doing any distinctive trick.
    Moreover, The contents are masterpiece. you’ve done a great task in this topic!

  3. Hi there! Your website is running slowly to me, this went on just like a minute to finally load, I do not know if
    it is just me or perhaps your web page although google and yahoo worked for me.
    On the other hand thank you for submitting beautiful blog post.
    Everyone who came to this great site must have observed this article honestly
    beneficial. I must mention that you have done brilliant
    work with this and expect to check out many more amazing stuff from you.

    To get more information by posts which you write-up, I have saved
    this web site.

  4. Hello Yahoo works fine yet your website is starting steadily which
    had taken about a minute to be able to load, I don’t know whether or not it’s my problem or perhaps
    your website issue. Nevertheless, I must appreciate you for attaching beautiful blog post.
    I’m assuming this has become beneficial to plenty of people who came on this page.
    I need to mention that you have done fantastic job with this and also expect to find
    much more amazing content from you. I ‘ve got your site saved to my bookmarks to look at
    new stuff you post.

  5. Hello Your entire site starts up really slow for my situation, I don’t know who’s issue is that however , twitter and facebook
    starts fairly immediate. However , I appreciate you for creating marvelous articles.
    I believe it really has already been literally useful to user who seem to visit here.
    I ought to state that you really have done excellent job with this and expect to see much more wonderful content through you.

    I already have you saved to bookmarks to check new stuff
    you post.

  6. Even a professional home gardener could possibly be baffled by new terminology
    and methodology when switching for an organic approach.

    including fetching a ball or perhaps a good game of hide-and-seek.
    Attach chains to three sides of 2 or 3 planters and connect the chains together to make a
    tiered garden.

  7. yoս are гeally a ϳust rigɦt webmaster. The site loading pace іs amazing.
    It ѕeems tthat ƴߋu’rе doing any distinctive trick.
    Αlso, The copntents aге masterpiece. уou have performed a magnificent process іn thjis matter!

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