如何在Windows / IIS服务器上获取当前页面的完整URL?

我将WordPress安装移到了Windows / IIS服务器上的新文件夹中。 我设置了301redirect在PHP中,但似乎并没有工作。 我的posturl格式如下:

http:://www.example.com/OLD_FOLDER/index.php/post-title/ 

我无法弄清楚如何抓取URL的/post-title/部分。

$_SERVER["REQUEST_URI"] – 大家似乎build议 – 返回一个空string。 $_SERVER["PHP_SELF"]刚刚返回index.php 。 这是为什么,我该如何解决?

也许,因为你在IIS下,

 $_SERVER['PATH_INFO'] 

是你想要的,基于你用来解释的URL。

对于Apache,你可以使用$_SERVER['REQUEST_URI']

 $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; 

对于Apache:

 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] 

赫尔曼评论说,你也可以使用HTTP_HOST而不是SERVER_NAME 。 看到这个相关的问题进行了充分的讨论。 总之,你可能还可以使用。 这里是“主机”版本:

 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] 

为偏执狂/为什么它重要

通常,我在VirtualHost设置ServerName ,因为我希望是网站的规范forms。 $_SERVER['HTTP_HOST']根据请求标头设置。 如果服务器响应在该IP地址处的任何/所有域名,则用户可能欺骗该头部,或者更糟糕的是,有人可能将DNSlogging指向您的IP地址,然后您的服务器/网站将用dynamicbuild立在不正确的url上的链接。 如果你使用后一种方法,你也应该configuration你的vhost或者设置一个.htaccess规则来强制你想要发布的域名,例如:

 RewriteEngine On RewriteCond %{HTTP_HOST} !(^stackoverflow.com*)$ RewriteRule (.*) https://stackoverflow.com/$1 [R=301,L] #sometimes u may need to omit this slash ^ depending on your server 

希望有所帮助。 这个答案的真正意义在于为那些最终在这里寻找一种方式来获得完整的URL的人提供第一行代码:)

$_SERVER['REQUEST_URI']在IIS上不起作用,但是我确实发现了这个: http : //neosmart.net/blog/2006/100-apache-compliant-request_uri-for-iis-and-windows/有希望。

使用这个类来获得URL的作品。

 class VirtualDirectory { var $protocol; var $site; var $thisfile; var $real_directories; var $num_of_real_directories; var $virtual_directories = array(); var $num_of_virtual_directories = array(); var $baseURL; var $thisURL; function VirtualDirectory() { $this->protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http'; $this->site = $this->protocol . '://' . $_SERVER['HTTP_HOST']; $this->thisfile = basename($_SERVER['SCRIPT_FILENAME']); $this->real_directories = $this->cleanUp(explode("/", str_replace($this->thisfile, "", $_SERVER['PHP_SELF']))); $this->num_of_real_directories = count($this->real_directories); $this->virtual_directories = array_diff($this->cleanUp(explode("/", str_replace($this->thisfile, "", $_SERVER['REQUEST_URI']))),$this->real_directories); $this->num_of_virtual_directories = count($this->virtual_directories); $this->baseURL = $this->site . "/" . implode("/", $this->real_directories) . "/"; $this->thisURL = $this->baseURL . implode("/", $this->virtual_directories) . "/"; } function cleanUp($array) { $cleaned_array = array(); foreach($array as $key => $value) { $qpos = strpos($value, "?"); if($qpos !== false) { break; } if($key != "" && $value != "") { $cleaned_array[] = $value; } } return $cleaned_array; } } $virdir = new VirtualDirectory(); echo $virdir->thisURL; 

加:

 function my_url(){ $url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; echo $url; } 

然后调用my_url函数。

我使用以下函数来获取当前的完整URL。 这应该在IIS和Apache上工作。

 function get_current_url() { $protocol = 'http'; if ($_SERVER['SERVER_PORT'] == 443 || (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')) { $protocol .= 's'; $protocol_port = $_SERVER['SERVER_PORT']; } else { $protocol_port = 80; } $host = $_SERVER['HTTP_HOST']; $port = $_SERVER['SERVER_PORT']; $request = $_SERVER['PHP_SELF']; $query = isset($_SERVER['argv']) ? substr($_SERVER['argv'][0], strpos($_SERVER['argv'][0], ';') + 1) : ''; $toret = $protocol . '://' . $host . ($port == $protocol_port ? '' : ':' . $port) . $request . (empty($query) ? '' : '?' . $query); return $toret; } 

REQUEST_URI由Apache设置,所以你不会使用IIS。 尝试在$ _SERVER上执行var_dump或print_r,并查看可以使用的值。

URL的posttitle部分位于index.php文件之后,这是提供友好URL而不使用mod_rewrite的常用方式。 因此,标题实际上是查询string的一部分,所以你应该可以使用$ _SERVER ['QUERY_STRING']

在使用$_SERVER['REQUEST_URI']的PHP页面顶部使用以下行。 这将解决您的问题。

 $_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'] . '?' . $_SERVER['argv'][0]; 

哦,一个片段的乐趣!

 if (!function_exists('base_url')) { function base_url($atRoot=FALSE, $atCore=FALSE, $parse=FALSE){ if (isset($_SERVER['HTTP_HOST'])) { $http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http'; $hostname = $_SERVER['HTTP_HOST']; $dir = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); $core = preg_split('@/@', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY); $core = $core[0]; $tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s"); $end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir); $base_url = sprintf( $tmplt, $http, $hostname, $end ); } else $base_url = 'http://localhost/'; if ($parse) { $base_url = parse_url($base_url); if (isset($base_url['path'])) if ($base_url['path'] == '/') $base_url['path'] = ''; } return $base_url; } } 

它有如下美丽的回报:

 // A URL like http://stackoverflow.com/questions/189113/how-do-i-get-current-page-full-url-in-php-on-a-windows-iis-server: echo base_url(); // Will produce something like: http://stackoverflow.com/questions/189113/ echo base_url(TRUE); // Will produce something like: http://stackoverflow.com/ echo base_url(TRUE, TRUE); || echo base_url(NULL, TRUE); //Will produce something like: http://stackoverflow.com/questions/ // And finally: echo base_url(NULL, NULL, TRUE); // Will produce something like: // array(3) { // ["scheme"]=> // string(4) "http" // ["host"]=> // string(12) "stackoverflow.com" // ["path"]=> // string(35) "/questions/189113/" // } 

大家都忘记了http_build_url ?

 http_build_url($_SERVER['REQUEST_URI']); 

当没有parameter passing给http_build_url ,它将自动采用当前的URL。 我期望REQUEST_URI也包含在内,尽pipe它似乎是包含GET参数所必需的。

上面的例子将返回完整的URL。

我已经使用了下面的代码,我得到了正确的结果…

 <?php function currentPageURL() { $curpageURL = 'http'; if ($_SERVER["HTTPS"] == "on") { $curpageURL.= "s"; } $curpageURL.= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $curpageURL.= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $curpageURL.= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $curpageURL; } echo currentPageURL(); ?> 

在我的apache服务器,这给了我所需的完整格式的完整URL:

 $_SERVER["SCRIPT_URI"] 

反向代理支持!

有点更强大。 注意它只能在5.3或更高版本上运行。

 /* * Compatibility with multiple host headers. * Support of "Reverse Proxy" configurations. * * Michael Jett <mjett@mitre.org> */ function base_url() { $protocol = @$_SERVER['HTTP_X_FORWARDED_PROTO'] ?: @$_SERVER['REQUEST_SCHEME'] ?: ((isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https" : "http"); $port = @intval($_SERVER['HTTP_X_FORWARDED_PORT']) ?: @intval($_SERVER["SERVER_PORT"]) ?: (($protocol === 'https') ? 443 : 80); $host = @explode(":", $_SERVER['HTTP_HOST'])[0] ?: @$_SERVER['SERVER_NAME'] ?: @$_SERVER['SERVER_ADDR']; // Don't include port if it's 80 or 443 and the protocol matches $port = ($protocol === 'https' && $port === 443) || ($protocol === 'http' && $port === 80) ? '' : ':' . $port; return sprintf('%s://%s%s/%s', $protocol, $host, $port, @trim(reset(explode("?", $_SERVER['REQUEST_URI'])), '/')); }