Steven Vachon

Absolute-to-Relative URLs

2 comments

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

    • Before: http://example.com/test/another-test/#anchor
    • After: ../another-test/#anchor
    • Before: http://example.com/wp-content/themes/twentyten/style.css
    • After: /wp-content/themes/twentyten/style.css
    • Before: https://example.com/wp-content/themes/twentyten/style.css
    • After: https://example.com/wp-content/themes/twentyten/style.css
    • Before: http://google.com/test/
    • After: //google.com/test/
    • 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

<?php
echo absolute_to_relative_url('http://your-domain.com/dir/');
?>

… and/or:

<?php
$instance = new Absolute_to_Relative_URLs('http://remote-site.com/');
 
echo $instance->relate_url('http://remote-site.com/dir/', false, 0);
?>


Comments

2 comments Jump to comment form

Michael
1Michael

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 http://www.website.com/about-us/ I want to remain in www. When I am in mike.website.com/about-us/ 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.

Thanks!

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 http://www.domain.com as the base URL and have sub-domains running as copies, then all links will be pointing to http://www.domain.com. 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.

Top

Leave a Reply

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