Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert relative URL to absolute URL

Tags:

regex

bash

url

Input:

  • Base URL: www.example.com/1/2/index.php
  • Relative URL: ../../index.php

Output:

  • Absolute URL: www.example.com/index.php

It would be perfect, of it would be done using sed.

As I understand, this regex should delete one somefolder/ in for every ../ in the URL.

like image 702
JohnDow Avatar asked Jan 27 '26 10:01

JohnDow


2 Answers

realpath is a quick but slightly hacky way to do what you want.
(Actually, I'm surprised that it doesn't deal properly with URLs; it treats them as plain old filesystem paths.)
~$ realpath -m http://www.example.com/1/2/../../index.php => ~$ /home/username/http:/www.example.com/index.php
The -m (for "missing") says to resolve the path even if components of it don't actually exist on the filesystem.
So you'll still have to strip off the actual filesystem part of that (which will just be $(pwd). And note that the slash-slash for the protocol was also canonicalized to a single slash. So you might be better off to leave the "http://" off of your input and just prepend it to your output instead.
See man 1 realpath for the full story. Or info coreutils 'realpath invocation' for a more verbose full story, if you have the info system installed.

like image 88
Edward Avatar answered Jan 30 '26 01:01

Edward


Using sed inside bash

#!/bin/bash

base_url='www.example.com/1/2/index.php'
rel_url='../../index.php'

str="${base_url};${rel_url}"
str=$(echo $str | sed -r 's#/[^/]*;#/#')
while [ ! -z $(echo $str | grep '\.\.') ]
do
  str=$(echo $str | sed -r 's#\w+/\.\./##')
done
abs_url=$str

echo $abs_url

Output:

www.example.com/index.php
like image 31
jkshah Avatar answered Jan 30 '26 03:01

jkshah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!