没有htaccess的PHP中的URL重写 [英] URL rewriting in PHP without htaccess
问题描述
网站正在无法访问.htaccess
文件的网络主机上运行.但是,我想对用户友好的URL进行URL重写.
Website is running on a web host where we don't have access to a .htaccess
file. However, I want to do URL rewriting for user friendly URLs.
例如原始网址
www.example.com/file?q=name
预期网址
www.example.com/file/name
推荐答案
正如其他人所说,只需使用/index.php/nice/looking/url
之类的链接即可.
网址中间的"index.php"可能看起来有些奇怪,但是我认为没有.htaccess可能无法使其看起来更好
As other people said, just use links like /index.php/nice/looking/url
.
The "index.php" in the middle of the URL might look a little strange, but I don't think it's possible to have it look better without .htaccess
否则,您可以要求托管服务商将任何URL重定向到/index.php,以便您可以处理URL重写而无需在URL中输入/index.php
.
Else, you could ask your hoster to redirect any URL to /index.php so that you can handle URL rewriting without having /index.php
in your URL.
然后,您可以使用正则表达式匹配来检测要包含的文件.
preg_match('@[/]{1}([a-zA-Z0-9]+)@', $_SERVER["PATH_INFO"], $matches)
($ matches将包含数组中URL的所有部分")
Then you can just use a regex match to detect what file to include.
preg_match('@[/]{1}([a-zA-Z0-9]+)@', $_SERVER["PATH_INFO"], $matches)
($matches will contain all "parts" of the url in an array)
在添加文件时要小心,请使用白名单,以确保没有人能够加载内部文件.
Be careful with including the files, use a whitelist so you're sure nobody would be able to load internal files.
这篇关于没有htaccess的PHP中的URL重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!