我将WordPress安装移动到Windows/IIS服务器上的新文件夹.我在PHP中设置301重定向,但它似乎没有工作.我的帖子网址格式如下:
http:://www.example.com/OLD_FOLDER/index.php/post-title/
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何抓取/post-title/URL 的一部分.
$_SERVER["REQUEST_URI"] - 每个人似乎都建议 - 返回一个空字符串.$_SERVER["PHP_SELF"]刚刚回来index.php.为什么会这样,我该如何解决?
Vin*_*vic 135
也许,因为你在IIS下,
$_SERVER['PATH_INFO']
Run Code Online (Sandbox Code Playgroud)
是您想要的,基于您用来解释的URL.
对于Apache,你可以使用$_SERVER['REQUEST_URI'].
Tyl*_*ter 63
$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;
Run Code Online (Sandbox Code Playgroud)
cwd*_*cwd 36
'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']
Run Code Online (Sandbox Code Playgroud)
您也可以使用Herman评论HTTP_HOST而不是SERVER_NAME.有关完整讨论,请参阅此相关问题.简而言之,使用其中任何一个都可以.这是'主机'版本:
'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
Run Code Online (Sandbox Code Playgroud)
通常情况下,我设置ServerName的VirtualHost,因为我想这是典型的网站的形式.该$_SERVER['HTTP_HOST']基于请求头设置.如果服务器响应该IP地址上的任何/所有域名,则用户可以欺骗标题,或者更糟糕的是,有人可能将DNS记录指向您的IP地址,然后您的服务器/网站将提供具有动态的网站建立在错误网址上的链接.如果您使用后一种方法,您还应配置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
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.这个答案的真正目的只是为那些在搜索到获取完整URL的方法时最终到达这里的人提供第一行代码:)
Gre*_*reg 11
$_SERVER['REQUEST_URI'] 在IIS上不起作用,但我确实发现了这个: http://neosmart.net/blog/2006/100-apache-compliant-request_uri-for-iis-and-windows/听起来很有希望.
小智 9
使用此类可以获取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;
Run Code Online (Sandbox Code Playgroud)
小智 7
加:
function my_url(){
$url = (!empty($_SERVER['HTTPS'])) ?
"https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] :
"http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
echo $url;
}
Run Code Online (Sandbox Code Playgroud)
然后只需调用该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;
}
Run Code Online (Sandbox Code Playgroud)