Steven Vachon

Absolute-to-Relative URLs


The title says it all. So, if you were to use this library on a website like, you would get results like these:

    • Before:
    • After: ../another-test/#anchor
    • Before:
    • After: /wp-content/themes/twentyten/style.css
    • Before:
    • After:
    • Before:
    • After: //
    • Before: ../../../../../../../../#anchor
    • After: /#anchor
    • After ($output_type=1): ../../#anchor

All string parsing. No directory browsing.

(New) For Node.js

For Standard PHP

For WordPress

How To Use It

echo absolute_to_relative_url('');

… and/or:

$instance = new Absolute_to_Relative_URLs('');
echo $instance->relate_url('', false, 0);


2 comments Jump to comment form


You might be one of the only people out there who can help me.

I need to make all of my links relative links. I’m using a 3rd level sub-domain to create what we call “replicated websites” for our independent sales reps.

The site is essentially the same, but when I am in I want to remain in www. When I am in I want to remain in that site. I downloaded both of your plugins and activated them, but it isn’t getting rid of the full path link and giving me a relative link. Any ideas????

Thanks I appreciate any help you can give me.


Steven Vachon
2Steven Vachon

My plugin is designed to shorten the length of URLs within a site based on the current domain and the content. If you’re running a single WordPress installation with as the base URL and have sub-domains running as copies, then all links will be pointing to Therefore, my plugin will leave them as absolute URLs for all “replicated websites”. Changing this would go beyond the original scope of my plugin. So, you have 2 options: rewrite my plugin for your own [legal] use, or have multiple WordPress installations. Feel free to modify my plugin and release it under a new name to suit its new purpose; others may be in your shoes.


Leave a Reply

Comments welcome! Name & email required; email always kept private. Please use basic markup. Wrap code with <code> tags.