转换相对URL绝对URL [英] Convert relative URL to absolute URL

查看:225
本文介绍了转换相对URL绝对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入:


  • 基础网址: www.example.com/1/2/index.php

  • 相对网址: ../../的index.php

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

输出:


  • 绝对网址: www.example.com/index.php

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

这将是完美的,它会使用来完成的 SED

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

据我了解,这个正则表达式应该删除一个 somefolder / 在URL中的每个 ../

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

推荐答案

如果你唯一的要求就是打开 .. 进入上一级,那么这是一种可能解。它不使用常规的前pressions或sed或为此事JVM)

If your only requirement is to turn .. into "up one level" then this is a possible solution. It doesn't use regular expressions or sed, or a JVM for that matter ;)

#!/bin/bash                                                                                                                                

domain="www.example.com"
origin="1/2/3/4/index.php"
rel="../../index.php"

awk -v rel=$rel -v origin=$origin -v file=$(basename $rel) -v dom=$domain '                                                                
BEGIN {                                                                                                                                    
    n = split(rel, a, "/")                                                                                                                 
    for(i = 1; i <= n; ++i) {                                                                                                              
        if(a[i] == "..") ++c                                                                                                               
    }                                                                                                                                      
    abs = dom                                                                                                                              
    m=split(origin, b, "/")                                                                                                                
    for(i = 1; i < m - c; ++i) {                                                                                                           
        abs=abs"/"b[i]                                                                                                                     
    }                                                                                                                                      
    print abs"/"file                                                                                                                       
}'

这是另一种方法,使用 AWK ,信贷爱德华为提的真实路径-m

An alternative approach to using awk, credit to Edward for mentioning realpath -m:

#!/bin/bash                                                                                                                                

rel="../../index.php"
origin="www.example.com/1/2/index.php"

directory=$(dirname $origin)
fullpath=$(realpath -m "$directory/$rel")
echo ${fullpath#$(pwd)/}

这篇关于转换相对URL绝对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆