开发者

How to compare current website url to the website url in the database?

What is the best way to compare a current website url in your address bar with the url saved in the database?

What I want to do is make it so if the url in the database is http://www.domain.com/ but the user types in http://domain.com/ it will 301 redirect to the dom开发者_如何学运维ain saved in the database...

The same will happen if the domain saved is http://domain.com/ and they enter http://www.domain.com/... I want it to redirect to http://domain.com/

I don't want to do this with htaccess. I want to actually build it into my PHP script. Wordpress has done this but I can't find the code that they use to do it...

Thanks for your help!


Something like this might work.

<?php
if ('http://' . $_SERVER['HTTP_HOST'] . '/' != $url_from_db) {
   header('HTTP/1.1 301 Moved Permanently'); 
   header('Location: ' . $url_from_db . $_SERVER['REQUEST_URI']);
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜