检索wordpress页面的原始URL

时间:2012-07-10 19:22:03

标签: php wordpress

我知道,wordpress将密切匹配的网址重定向到其原始网址。但是,我需要在代码执行的最开始就知道实际的网址。有可能吗?

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

2)在代码中实际进行此测试和重定向的地方吗?

3)是否有任何钩子可用于添加插件来控制这种情况?

提前致谢。

示例:

假设,我在此网址上发布了一条帖子:“http://mydomain.com/example-post-page”

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

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

3 个答案:

答案 0 :(得分:1)

如果您尝试在PHP中执行此操作,则可以询问服务器所请求的URL是什么。尝试访问$ _SERVER []并查看它是否具有您要查找的内容。 (protip:在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']从您脚本中的任何位置访问。 我希望这很有帮助!

答案 1 :(得分:0)

答案 2 :(得分:0)

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

1)收到大约。 2)使用此约定网址发出cURL请求。 3)获得响应网址。 4)返回响应URL。

以下代码:

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;
}}