在没有 htaccess 的情况下用 PHP 重写 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2126762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:09:27  来源:igfitidea点击:

URL rewriting in PHP without htaccess

phpweb-hosting

提问by Vivart

Website is running on a web host where we don't have access to a .htaccessfile. However, I want to do URL rewriting for user friendly URLs.

网站在我们无法访问.htaccess文件的网络主机上运行。但是,我想为用户友好的 URL 进行 URL 重写。

e.g. Original URL

例如原始网址

www.example.com/file?q=name

expected URL

预期网址

www.example.com/file/name

采纳答案by Daan

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

正如其他人所说,只需使用像/index.php/nice/looking/url.
URL 中间的“index.php”可能看起来有点奇怪,但我认为没有 .htaccess 就不可能让它看起来更好

Else, you could ask your hoster to redirect any URL to /index.php so that you can handle URL rewriting without having /index.phpin your URL.

否则,您可以要求您的托管商将任何 URL 重定向到 /index.php,以便您无需输入 URL 即可处理 URL 重写/index.php

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)

然后您可以使用正则表达式匹配来检测要包含的文件。
preg_match('@[/]{1}([a-zA-Z0-9]+)@', $_SERVER["PATH_INFO"], $matches)($matches 将包含数组中 url 的所有“部分”)

Be careful with including the files, use a whitelistso you're sure nobody would be able to load internal files.

小心包含文件,使用白名单,这样你就可以确定没有人能够加载内部文件。

回答by Pentium10

as Alix Axel suggested you can use

正如 Alix Axel 建议您可以使用

www.example.com/index.php/file/name

then you will use $_SERVER['REQUEST_URI']to process the URL.

然后您将用于$_SERVER['REQUEST_URI']处理 URL。

回答by Alix Axel

Your best bet will be to have URLs such as this:

最好的办法是拥有这样的 URL:

www.example.com/index.php/file/name

You'll to rewrite your PHP code though.

不过,您将重写您的 PHP 代码。

回答by Gumbo

If you have an Apache server and AcceptPathInfois enabled, then you can use the URL you wrote. A request of /file/namewill then be automatically rewritten to /filewith the PATH_INFOvalue of /nameif /fileis a regular file.

如果您有一个 Apache 服务器并且启用了AcceptPathInfo,那么您可以使用您编写的 URL。/file/name然后将/file使用if的PATH_INFO值自动将请求重写为常规文件。/name/file

回答by Daniel Nyamasyo

Try this

尝试这个

www.example.com/file?q=name

www.example.com/file?q=name

to

www.example.com/name(better than www.example.com/file/name)

www.example.com/name(优于www.example.com/file/name

Open your .htaccessfrom your project root folder and add this code

.htaccess从您的项目根文件夹打开您的并添加此代码

Options +FollowSymLinks

RewriteEngine On
RewriteRule \.(js|css|jpe?g|png|gif)$ - [L]


RewriteRule "^([ \w-]+)/?$" /file?q= [L,QSA]

This solves your problem. See the post how to rewrite URL in .htaccess

这可以解决您的问题。请参阅如何重写 URL 中的帖子.htaccess