2

我知道,wordpress 会将紧密匹配的 url 重定向到其原始 url。但是,我需要在代码执行的一开始就知道实际的 url。是否可以?

1)如果有任何方法将 url 作为参数并返回其原始 wordpress url ,那会很好,但不确定它是否存在。有没有类似的东西?

2) 请问这个测试和重定向在代码中的哪个位置?

3)有没有什么钩子可以用来添加插件来控制这种情况?

提前致谢。

例子:

假设,我在这个网址上有一个帖子:“http://mydomain.com/example-post-page”

现在,如果我尝试通过“http://mydomain.com/example-post-page/”访问它,它会将您重定向到原始的第一个 url。对于 url 中的其他几个小更改也是如此,所有这些都会将您重定向到实际的永久链接 url。

我的目标是控制这种重定向。这就是为什么,当我在 "http://mydomain.com/example-post-page/" 上时,我想知道(我在根 index.php 上的代码)这是原始永久链接还是其他东西,在它重定向之前。

4

3 回答 3

2

如果您尝试在 PHP 中执行此操作,您可以询问服务器请求的 URL 是什么。尝试访问 $_SERVER[ ] 看看这是否有你要找的东西。(提示:要在 php 中查看整个变量,请使用print_r($_SERVER);

我在我的 wordpress 网站上执行了命令并返回:

 Array
(
    [SERVER_SOFTWARE] => Apache
    [REQUEST_URI] => /wordpress_NEWURL/cookbook/
    [REDIRECT_SCRIPT_URL] => /wordpress_NEWURL/cookbook/
    [REDIRECT_SCRIPT_URI] => http://openmdao.org/wordpress_NEWURL/cookbook/
    [REDIRECT_HTTPS] => off
    [REDIRECT_X-FORWARDED-PROTO] => http
    [REDIRECT_X-FORWARDED-SSL] => off
    [REDIRECT_STATUS] => 200
    [SCRIPT_URL] => /wordpress_NEWURL/cookbook/
    [SCRIPT_URI] => http://openmdao.org/wordpress_NEWURL/cookbook/
    [HTTPS] => off
    [X-FORWARDED-PROTO] => http
    [X-FORWARDED-SSL] => off
    [HTTP_HOST] => openmdao.org
    [HTTP_X_FORWARDED_HOST] => openmdao.org
    [HTTP_X_FORWARDED_SERVER] => openmdao.org
    [HTTP_X_FORWARDED_FOR] => 128.156.10.80
    [HTTP_HTTP_X_FORWARDED_PROTO] => http
    [HTTP_HTTPS] => off
    [HTTP_X_FORWARDED_PROTO] => http
    [HTTP_X_FORWARDED_SSL] => off
    [HTTP_CONNECTION] => close
    [CONTENT_LENGTH] => 92
    [HTTP_CACHE_CONTROL] => max-age=0
    [HTTP_ORIGIN] => http://openmdao.org
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5
    [CONTENT_TYPE] => application/x-www-form-urlencoded
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [HTTP_REFERER] => http://openmdao.org/wordpress_NEWURL/cookbook/
    [HTTP_ACCEPT_ENCODING] => gzip,deflate,sdch
    [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.8
    [HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.3
    [HTTP_COOKIE] => wp-settings-1=m7%3Do%26m11%3Do%26wplink%3D0%26editor%3Dtinymce%26hidetb%3D1%26align%3Dright; wp-settings-time-1=1341576701; wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_7f72de226f3f83cb831f7a36bd420125=admin%7C1342021433%7C1c54f9729f967300e8d1ecc80eea7e38; w3tc_referrer=http%3A%2F%2Fopenmdao.org%2Fwordpress_test%2Fsdk-1.5.6.2%2F_samples%2F; wp-settings-1=m7%3Do%26m11%3Do%26wplink%3D0; wp-settings-time-1=1339595284; greeting_set=True; _gauges_unique_month=1; _gauges_unique_year=1; _gauges_unique=1; wordpress_test_cookie=WP+Cookie+check; PHPSESSID=0302b71abfbfd5a8d7854ac168aba2f9; sessionid=7b87104b87d95624cab0fe282079aa07; csrftoken=e72c25d0cdc3a6682c89e4680742ff3b; __utma=192130932.1535996015.1339438190.1341938618.1341945509.58; __utmc=192130932; __utmz=192130932.1339605598.7.2.utmcsr=rss|utmccn=new-website|utmcmd=rss
    [HTTP_VIA] => 1.1 www-fw.grc.nasa.gov 8B58912A
    [PATH] => /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/home/ph/trunk/python-hosting.com/new_site/monitor:/home/ph/trunk/python-hosting.com/new_site/manual_scripts:/root/bin
    [SERVER_SIGNATURE] => 
    [SERVER_NAME] => openmdao.org
    [SERVER_ADDR] => 127.0.0.1
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 128.156.10.80
    [DOCUMENT_ROOT] => /home/openmdao/webapps/_
    [SERVER_ADMIN] => [no address given]
    [SCRIPT_FILENAME] => /home/openmdao/webapps/wp_test/index.php
    [REMOTE_PORT] => 51907
    [REDIRECT_URL] => /wordpress_NEWURL/cookbook/
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.0
    [REQUEST_METHOD] => POST
    [QUERY_STRING] => 
    [SCRIPT_NAME] => /wordpress_NEWURL/index.php
    [PHP_SELF] => /wordpress_NEWURL/index.php
    [REQUEST_TIME] => 1341948361
    [argv] => Array
        (
        )

    [argc] => 0
)

$_SERVER['PARAMATER'] 可以从脚本中的任何位置访问这些参数中的任何一个。我希望这可以帮到你!

于 2012-07-10T19:25:30.780 回答
0

你是说the_permalink吗?http://codex.wordpress.org/Function_Reference/the_permalink

于 2012-07-10T20:07:18.990 回答
0

好的,最后我通过以下方式完成了它,对我来说似乎没问题:

1)收到大约网址。2) 使用这个近似 url 发出 cURL 请求。3)获取响应网址。4)返回响应网址。

代码如下:

function get_actual_url($approx_url){
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, $approx_url);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlObj, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curlObj, CURLOPT_ENCODING , "gzip");
curl_setopt($curlObj, CURLOPT_TIMEOUT,180);

$content = curl_exec($curlObj);

$info = curl_getinfo($curlObj);
curl_close($curlObj);

if(!empty($info) && !empty($info["url"])){
    return $info["url"];
}
else {
    return $approx_url;
}}
于 2012-07-14T19:24:45.967 回答