最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

urls - echo post-permalink without http and www

programmeradmin1浏览0评论

I have this line …

<div class="permalink"><?php the_permalink(); ?></div>

and the result on my page looks like this …


I guess it could also look like this …


However I'd like to have just mysite/whatever/post-or-so without the http:// or www in front of it.

What is the best and easiest way to do so?

Don't get me wrong, this has nothing todo with rewriting permalinks or whatsoever. Just a simple echo of the_permalink() on my page that is not handled as link but as normal text. And in this case I would like to get rid of the http or www.

I have this line …

<div class="permalink"><?php the_permalink(); ?></div>

and the result on my page looks like this …

http://mysite/whatever/post-or-so

I guess it could also look like this …

http://www.mysite/whatever/post-or-so

However I'd like to have just mysite/whatever/post-or-so without the http:// or www in front of it.

What is the best and easiest way to do so?

Don't get me wrong, this has nothing todo with rewriting permalinks or whatsoever. Just a simple echo of the_permalink() on my page that is not handled as link but as normal text. And in this case I would like to get rid of the http or www.

Share Improve this question asked Sep 4, 2012 at 14:44 mathiregistermathiregister 1,54313 gold badges55 silver badges78 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

use get_permalink instead of the_permalink and manipulate it however you'd like via php.

Like @milo suggested you can manipulate the return of get_permalink(). This can easily be done via several php string functions, here in use is str_replace(). If you have the need to remove both http:// and https:// give an array of needles to str_replace().

$permalink = get_permalink();
$find = array( 'http://', 'https://' );
$replace = '';
$output = str_replace( $find, $replace, $permalink );
echo '<p>' . $output . '</p>';

The above isn't taking care of the www(.) part, but the principle should be clear.

Another possibility for manipulation are php PCRE (Perl Compatible Regular Expressions) functions, here used preg_replace().

$permalink = get_permalink();
$find_h = '#^http(s)?://#';
$find_w = '/^www\./';
$replace = '';
$output = preg_replace( $find_h, $replace, $permalink );
$output = preg_replace( $find_w, $replace, $output );
echo '<p>' . $output . '</p>';

There is a simple method.

$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] will return the absolute url without http(s) in the beginning.

发布评论

评论列表(0)

  1. 暂无评论