简体   繁体   English

网址末尾的“正斜杠”是否有任何含义?

[英]Does a trailing “forward slash” have any meaning in a URL?

For example: 例如:

http://www.example.com/index.php?arg1=val1&arg2/

The ? ? specifies the start of GET values, but does the trailing / have any meaning to a URL? 指定GET值的开始,但是尾随/对URL有意义吗?

Reason I'm asking is because this is the first time I've tried it on my website, and all my internal links to CSS, images, etc. (which were all relative links to the current directory) didn't work, and so I need to provide the full path to get them to work in case a user types a URL with a trailing / . 我问的原因是因为这是我第一次在网站上尝试过它,而我所有到CSS,图像等的内部链接(都是到当前目录的相对链接)不起作用,并且所以我需要提供完整的路径来使它们工作,以防万一用户键入带有尾随/的URL。

I'm thinking of writing a rewrite rule to map index.php/ to index.php . 我正在考虑编写一个重写规则以将index.php/映射到index.php Is there any problem with this? 这有什么问题吗? Also, is there a way to generalise this to map anything.php/ to anything.php ? 另外,有没有一种方法可以将其概括为将anything.php/映射到anything.php

Server is Apache 2.4. 服务器是Apache 2.4。

Some people use trailing slashes to make clear that the url points to (for example) a folder. 某些人使用斜杠使URL指向(例如)文件夹。

When you want to point to a file, don't use a trailing slash. 当您要指向文件时,请不要使用斜杠。

Some Servers are faster when there is a trailing-slashes (when pointing to a folder). 带有斜杠(指向文件夹时)的某些服务器速度更快。 But that depends on the implementation. 但这取决于实现方式。

Laokoon's answer is correct, but that's about having a trailing "/" (forward slash) at the end of the "path" part of the URL, like: Laokoon的答案是正确的,但这是关于在URL的"path"部分的末尾带有尾随的"/" (正斜杠),例如:

http://www.example.com/
http://www.example.com/folder/

What you have described in your question is having a trailing "/" in the "query" part of the URL. 您在问题中描述的是在URL的"query"部分中带有尾随的"/"

Parts of the URL: "http://www.example.com/index.php?arg1=val1&arg2/"
http               Protocol
www.example.com    Subdomain.Domain
index.php          Path
?arg1=val1&arg2/   Query

In the case you describe: "http://www.example.com/index.php?arg1=val1&arg2/" , the trailing "/" does have a meaning, but perhaps not in the way that you might think. 在您描述以下情况的情况下: "http://www.example.com/index.php?arg1=val1&arg2/" ,结尾的"/"确实具有含义,但可能没有您可能想到的那样。

You have tagged your question as , so I will provide a PHP example. 您已将问题标记为 ,所以我将提供一个PHP示例。 It may be handled differently in other languages such as "Perl" . 在其他语言(例如"Perl" ,可能会有不同的处理方式。

There are 2 possibilities for a trailing "/" in the "query" : "query""/"结尾的可能性有2种:

1) If the trailing "/" follows an "=" , without an "&" in between, then the trailing "/" is part of the "value" of the "key" associated with the "=" : 1)如果结尾的"/"后面跟随"=" ,而中间没有"&" ,则结尾的"/"是与"="关联的"key""value"的一部分:

    "?arg1=val1/"            $_GET["arg1"] => 'val1/'
    "?arg1=/"                $_GET["arg1"] => '/'

2) If the "/" follows an "&" (or "?" ), without an "=" in between, then the "/" is part of the name of a "key" . 2)如果"/"后跟"&" (或"?" ),但中间没有"=" ,则"/""key"名称的一部分。 Further, if the "/" is at the very end (trailing), then the "value" of that "key" will be empty: 此外,如果"/"位于结尾(跟踪),则该"key""value" "key"将为空:

    "?arg1/=val1"            $_GET["arg1/"] => 'val1'
    "?arg1=val1&arg2/=val2"  $_GET["arg2/"] => 'val2'
    "?arg1/"                 $_GET["arg1/"] => ''
    "?arg1=val1&arg2/"       $_GET["arg2/"] => ''

If the trailing "/" immediately follows an "&" (or "?" ), then the trailing "/" is the name of a "key" : 如果结尾的"/" 紧跟"&" (或"?" )之后,则结尾的"/" "key"的名称:

    "?/"                     $_GET["/"] => ''
    "?arg1=val1&/"           $_GET["/"] => ''

Here is a PhpFiddle example: querytest2016-0713.php 这是一个PhpFiddle示例: querytest2016-0713.php

<?php

// http://main.xfiddle.com/55edba50/querytrailingslashtest/querytest2016-0713.php
echo "querytest2016-0713.php<br>\n";

echo 'HTTP_HOST ="'     . $_SERVER['HTTP_HOST']    . '"' . "<br>\n";
echo 'REQUEST_URI ="'   . $_SERVER['REQUEST_URI']  . '"' . "<br>\n";
echo 'QUERY_STRING ="'  . $_SERVER['QUERY_STRING'] . '"' . "<br>\n";
echo "<br>\n";
echo "Test using 'parse_url' and 'parse_str':<br>\n";

// $url = "http://www.example.com/index.php?arg1=val1&arg2/";
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

$qry = parse_url($url, PHP_URL_QUERY);
parse_str($qry);
parse_str($qry, $arr);

echo "url='"   . $url . "'<br>\n";
echo "query='" . $qry . "'<br>\n";

foreach ($arr as $key => $value) {
    echo $key . " => '" . $value . "'<br>\n";
}

echo "<br>\n";
echo "Test using '\$_GET':<br>\n";

foreach($_GET as $key => $value){
  echo $key . " => '" . $value . "'<br />\r\n";
}

?>

Example output: 输出示例:
querytest2016-0713.php?arg1=val1&arg2/ querytest2016-0713.php?ARG1 = VAL1&ARG2 /

querytest2016-0713.php
HTTP_HOST ="main.xfiddle.com"
REQUEST_URI ="/55edba50/querytrailingslashtest/querytest2016-0713.php?arg1=val1&arg2/"
QUERY_STRING ="arg1=val1&arg2/"

Test using 'parse_url' and 'parse_str':
url='http://main.xfiddle.com/55edba50/querytrailingslashtest/ ...
   ... querytest2016-0713.php?arg1=val1&arg2/'
query='arg1=val1&arg2/'
arg1 => 'val1'
arg2/ => ''

Test using '$_GET':
arg1 => 'val1'
arg2/ => ''

Example output: 输出示例:
querytest2016-0713.php?a=111&b=999&c=13579&arg1=901&arg2/=44&argx&whatever/=88&arg3/ querytest2016-0713.php?A = 111&B = 999&amp; C = 13579&ARG1 = 901&ARG2 / = 44&argx&无论/ = 88&ARG3 /

querytest2016-0713.php
HTTP_HOST ="main.xfiddle.com"
REQUEST_URI ="/55edba50/querytrailingslashtest/querytest2016-0713.php?a=111&b=999&c=13579&arg1=901&arg2/=44&argx&whatever/=88&arg3/"
QUERY_STRING ="a=111&b=999&c=13579&arg1=901&arg2/=44&argx&whatever/=88&arg3/"

Test using 'parse_url' and 'parse_str':
url='http://main.xfiddle.com/55edba50/querytrailingslashtest/ ...
    ... querytest2016-0713.php?a=111&b=999&c=13579&arg1=901&arg2/=44&argx&whatever/=88&arg3/'
query='a=111&b=999&c=13579&arg1=901&arg2/=44&argx&whatever/=88&arg3/'
a => '111'
b => '999'
c => '13579'
arg1 => '901'
arg2/ => '44'
argx => ''
whatever/ => '88'
arg3/ => ''

Test using '$_GET':
a => '111'
b => '999'
c => '13579'
arg1 => '901'
arg2/ => '44'
argx => ''
whatever/ => '88'
arg3/ => ''

Of course, if the query string is "manually" parsed in PHP using some other coded method, then the trailing "/" may have some other meaning or purpose, or it may be totally ignored. 当然,如果查询字符串是使用其他某种编码方法在PHP中“手动”解析的,则后缀"/"可能具有其他含义或用途,或者可能被完全忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM