php - 干净的 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2839666/
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
php - clean URL
提问by tibin mathew
I want to create a web site with pure PHP. I want to hide the url parameters. I.e. I want to make my web site with clean urls. Is there is any way to do this with out using any framework? Is cURL helpful to do this?
我想用纯 PHP 创建一个网站。我想隐藏 url 参数。即我想用干净的网址制作我的网站。有没有办法在不使用任何框架的情况下做到这一点?cURL 有助于做到这一点吗?
采纳答案by Mathias Bynens
See URL rewriting in PHP without .htaccessif you don't want to or can't use .htaccess, else refer to How to: URL rewriting in PHP?.
见URL重写PHP中没有.htaccess,如果你不想或者不能使用.htaccess,否则请参阅如何:在URL重写PHP?.
回答by Prabhu M
Just have a look on it...before you start your stuffs
在你开始你的东西之前看看它......
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
回答by Gumbo
First of all: It is not possible with PHP only (at least not the forms of URL I think of when reading clean URL). The web server needs to know how to handle requests and what requests are meant to be passed to your PHP script. Otherwise you will probably just get a 404 response.
首先:仅使用 PHP 是不可能的(至少不是我在阅读干净的 URL 时想到的 URL 形式)。Web 服务器需要知道如何处理请求以及要传递给 PHP 脚本的请求。否则,您可能只会收到 404 响应。
Because the default behavior of a web server is to just take the requested URL path and try to map it to an existing file below the document root. If a corresponding file was found, either the file's content is passed back to the client or – as in case of PHP files – the file's content is passed to an appropriate interpreter and the returned data is passed back to the client. And if the file was not found, well, it responds with the status code 404. So at some point you need to configure your web server.
因为 Web 服务器的默认行为是仅获取请求的 URL 路径并尝试将其映射到文档根目录下的现有文件。如果找到了相应的文件,则将文件内容传递回客户端,或者(如 PHP 文件)将文件内容传递给适当的解释器,并将返回的数据传递回客户端。如果未找到该文件,那么它会以状态代码 404 进行响应。因此,在某些时候您需要配置您的 Web 服务器。
But after that, when the request was passed to your PHP script, you sure can use just PHP to establish clean URLs. And I would rather suggest to do that with PHP than with web server utilities. Because your PHP application should know best how to handle a requested URL.
但是在那之后,当请求被传递到你的 PHP 脚本时,你肯定可以只使用 PHP 来建立干净的 URL。我更愿意建议使用 PHP 而不是 Web 服务器实用程序来做到这一点。因为您的 PHP 应用程序应该最了解如何处理请求的 URL。
In PHP, all required information are in the $_SERVERvariable:
在 PHP 中,所有必需的信息都在$_SERVER变量中:
$_SERVER['REQUEST_URI']holds the requested URL path and query (you can parse that withparse_url), and$_SERVER['PATH_INFO']holds the PATH_INFOif you're using that (see Apache'sAcceptPathInfodirective).
$_SERVER['REQUEST_URI']保存请求的 URL 路径和查询(您可以使用 解析它parse_url),以及$_SERVER['PATH_INFO']如果您正在使用它,则保存PATH_INFO(请参阅Apache 的AcceptPathInfo指令)。
回答by dave
From what I have read and understand of it, there's 2 ways you can do this:
根据我对它的阅读和理解,有两种方法可以做到这一点:
- The first being
mod_reritewhere everything seems to re fone through rewrite rules through the.htaccessfile fairly simple to do but can put big load on webserver with large sites - Use PHP to control the rerites this does use
.htaccessbut only to redirect everything back to the index.php where a dispatcher reroutes paths as necessary. There's a fantastic tutorial of this at phpvideotutorials.com the tutorial is called the tumblelog.
- 第一个是
mod_rerite一切似乎都通过重写规则重新编写的.htaccess文件相当简单,但可能会给具有大型站点的网络服务器带来巨大负载 - 使用 PHP 来控制 rerites 这确实使用,
.htaccess但只是将所有内容重定向回 index.php,其中调度程序根据需要重新路由路径。在 phpvideotutorials.com 上有一个很棒的教程,该教程称为 tumblelog。
回答by Daniel Nyamasyo
Try to rewrite url using phpand rewrite url using .HTACCESS.
尝试使用 php重写url 并使用 .HTACCESS重写url。
For example, original url,
例如,原始网址,
www.domain.com/item.php?product=Cars for sale in amazon
with php
用 php
www.domain.com/item.php?product=Cars-for-sale-in-amazon
and with .HTACCESS file
并带有 .HTACCESS 文件
www.domain.com/Cars-for-sale-in-amazon
回答by Your Common Sense
Nope, no curl or framework doing this. Nor php at all.
It is web serverwho deal with urls.
So, if you want fake urls, you have to set up your web server to redirect certain urls to certain scripts.
The most common way is to use Apache web server with mod_rewrite module
不,没有卷曲或框架这样做。也根本没有 php。处理 url 的
是Web 服务器。
因此,如果您想要假网址,则必须设置您的网络服务器以将某些网址重定向到某些脚本。
最常见的方法是使用带有 mod_rewrite 模块的 Apache Web 服务器

