简体   繁体   中英

How to get url in php?

I want to get the url in php. In my root directory "myproject"->book folder/index.php file

Kindly check what I am doing:

I am accessing the url on adressbar http://localhost:8080/myproject/book/index.php?id=myurl

PHP Code:

<?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/

The output will be: 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

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

Any idea or suggestion would be welcome.

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

It depends on your webserver, here are solutions for the main 3:

Apache mod_rewrite

nginx Rewrite rules

Microsoft IIS Rewrite rules

I think the hardest to accomplish are the ones on IIS, but all of them need some getting-used-to ... Enjoy

Most of the web frameworks have built in rewrite rules or routing with params instead of query parameters. 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 ***)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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