简体   繁体   English

如何在php中获取网址?

[英]How to get url in php?

I want to get the url in php. 我想在php中获取网址。 In my root directory "myproject"->book folder/index.php file 在我的根目录“ myproject”-> book folder / index.php文件中

Kindly check what I am doing: 请检查我在做什么:

I am accessing the url on adressbar http://localhost:8080/myproject/book/index.php?id=myurl 我正在访问地址栏上的url http:// localhost:8080 / myproject / book / index.php?id = myurl

PHP Code: PHP代码:

<?php

$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;

Works Perfectly
Output: http://localhost:8080/myproject/book/index.php?id=myurl

?>

If I want to access http://localhost:8080/myproject/book/ 如果我想访问http:// localhost:8080 / myproject / book /

The output will be: http://localhost:8080/myproject/book/ 输出将是: http:// localhost:8080 / myproject / book /

Till now all are working fine. 到现在为止一切正常。 But I want to get url like below, remove on my index.php file the actual url will be http://localhost:8080/myproject/book/index.php?id=myurl but I want when user hit url below 但是我想获得如下所示的网址,在我的index.php文件上删除实际的网址为http:// localhost:8080 / myproject / book / index.php?id = myurl,但是我希望当用户点击以下网址时

http://localhost:8080/myproject/book/myurl

It can access the url http://localhost:8080/myproject/book/index.php?id=myurl but on the addressbar still url show http://localhost:8080/myproject/book/myurl 它可以访问url http:// localhost:8080 / myproject / book / index.php?id = myurl,但在地址上仍然显示http:// localhost:8080 / myproject / book / myurl

Any idea or suggestion would be welcome. 任何想法或建议都将受到欢迎。

您应该在网站或nginx类似物根目录中的Apache网站服务器配置文件.htaccess中使用mod_rewrite指令。

It depends on your webserver, here are solutions for the main 3: 这取决于您的网络服务器,以下是主要解决方案3:

Apache mod_rewrite Apache mod_rewrite

nginx Rewrite rules nginx 重写规则

Microsoft IIS Rewrite rules Microsoft IIS 重写规则

I think the hardest to accomplish are the ones on IIS, but all of them need some getting-used-to ... Enjoy 我认为最难实现的是IIS,但是所有这些都需要一些入门知识。

Most of the web frameworks have built in rewrite rules or routing with params instead of query parameters. 大多数Web框架都内置了重写规则或使用参数而不是查询参数进行路由。 So depending on the language and framework you're using you may not have to go through the translation yourself (which can be a pain in the ***) 因此,根据您所使用的语言和框架,您可能不必自己进行翻译(这在***中可能很痛苦)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM