Can PHP access the .htaccess rewrite name of a file

和自甴很熟 提交于 2019-12-13 06:08:11

问题


My .htaccess works (not an expert, but do a lot of copy & paste).

A typical rewrite for one of my pages looks like this:

RewriteRule ^my-grills-parts-and-service/?$ parts-service.php [NC,L]

...where the url "http://www.mydomain.com/my-grills-parts-and-service/" will invoke the parts-service.php file.

However, I am using php to construct a fully qualified URL, from what is in the address bar. The problem is, the server variable $_SERVER['SCRIPT_NAME'] will return "parts-service.php", rather than what is actually in the address bar, "my-grills-parts-and-service/".

I want php to somehow read the literal rewrite string, rather than the actual filename.

So, I want php to construct a string like this:

$url = "http://www.mydomain.com/my-grills-parts-and-service/";

I hope I don't have to write a data table or switch or if block, because that would be doing everything twice (once in the .htaccess, and again in a php file).


回答1:


You can use $_SERVER['REQUEST_URI'] to get real url address.

Before using this method, check @regilero comment.



来源:https://stackoverflow.com/questions/18782189/can-php-access-the-htaccess-rewrite-name-of-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!