干净的 URL - 我该如何实现这一点?
我的主管最近告诉我,与其生成这样的页面 $pagecontent = "blahblahlbahblah"
,不如有一个页面从数据库中提取信息,并使用干净的 URL。
如何使用干净的 URL?我以前从未这样做过,谷歌似乎有多种不同的答案,但没有明确的答案。这是 PHP 的事情吗? HTML 的东西? Javascript 的东西?
My supervisor recently told me that instead of generating a page with something like this $pagecontent = "<html><body>blahblahlbahblah</body></html>"
, it was better to have a page pull information from the database, and use a clean URL.
How does one go about using clean URLs? I've never done this before, and Google seems to have multiple different answers, and nothing clear cut. Is it a PHP thing? An HTML thing? A Javascript thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您使用的网站托管在 Apache 下(大多数使用 PHP),那么它通常是一个 .htaccess 的东西,称为 mod_rewrite。 .htaccess 文件的一个简单示例是:
因此,在此示例中, http://example.com /aLink/?test=a 将被重定向到 http://example.com/index.php?query=aLink/&test=a。
然后,您可以使用 PHP 解析
$_GET['query']
变量。It's usually an .htaccess thing, called mod_rewrite, if you're using website is hosted under Apache (which, using PHP, most are). A quick example of a .htaccess file would be:
So, in this example, http://example.com/aLink/?test=a would be redirected to http://example.com/index.php?query=aLink/&test=a.
You can then use PHP to parse the
$_GET['query']
variable.干净的 URL 通常是使用 URL 重写来完成的,它们将
site.com?var=1&var2=2
转换为site.com/1/2
在 Apache 服务器上,这是使用 mod_rewrite 完成的。您可以尝试使用 Wordpress,它将生成正确的 Apache 文件和代码,用于将
?page=name&category=that
等 WordPress 内容重写为/name
。Clean URLs are typically accomplished using URL rewrites, where they turn
site.com?var=1&var2=2
intosite.com/1/2
On the Apache server, this is accomplished using mod_rewrite. You can experiment using Wordpress, which will generate the proper Apache files and code for rewriting Wordpress stuff like
?page=name&category=that
to/name
.我认为他的意思是,
你不想像
评论中提到的那样,根据网络服务器、网络应用程序设置,你可以免费获得它。 Apache 使用
mod_rewrite
例如I think what he meant is that instead of
you'd rather have
As mentioned in the comment, depending upon the webserver, webapp setup, you could get it for free. Apache uses
mod_rewrite
for e.g.使用 Apache 和 PHP,您也可以在不使用 mod_rewrite 的情况下完成此任务。如今似乎没有人关心运行 mod_rewrite 所需的开销,但如果您有兴趣充分发挥 Web 服务器的性能,您可能会喜欢另一种方法。
请参阅 http://www.evolt.org/Making_clean_URLs_with_Apache_and_PHP 了解使用 Apache 的 ForceType 指令的解决方案的说明。
With Apache and PHP, you can accomplish this without mod_rewrite as well. No one seems to care these days about the overhead needed to run mod_rewrite, but if you're interested in squeezing every bit of performance out of your web server, you might like the alternative method.
See
http://www.evolt.org/Making_clean_URLs_with_Apache_and_PHP
for an explanation of the solution using Apache's ForceType directive.干净的 URL 只是没有查询字符串的 URL。例如,您可能需要
http://example.com/foo
而不是http://example.com/index.php?page=foo
对于想要转向干净 URL 的大型现有网站来说,它们可能会变得非常复杂。正如 Matt H 和其他人所说,您可以使用 Apache 重写将现有 URL 更改为干净的 URL。 http://httpd.apache.org/docs/current/mod/mod_rewrite。 html 有关于 mod_rewrite 的完整文档,但您可能会通过示例学得更快。 jValdron 刚刚发布了一篇不错的文章。这个答案很快就会变成更快答案的大杂烩。有太多关于重写和使用 PHP 处理它们的文档,以至于这么宽泛的问题实际上不值得再次回答。
如果您有一个非常大的网站,其中有很多采用查询字符串的 php 页面,您将必须计划编写大量重写并确保您的链接不会损坏,或者对脚本的一次重写将处理重定向到正确的页面。
如果您要创建一个新网站,我会将您的 URL 重写为单个脚本,该脚本将根据 URL 获取内容。
这是 Drupal 的 .htaccess 文件中的一个著名示例。
然后
QSA
表示查询字符串追加,前面的L
表示最后立即停止重写过程,不再应用任何规则。
然后索引.php 可以通过 $_GET['q'] 访问 URL。
A clean URL is just a URL without a query string. So for example, instead of
http://example.com/index.php?page=foo
you might wanthttp://example.com/foo
They can become very complicated for large existing sites that want to move to clean URLs. As Matt H and others said, you can use Apache rewrite to change existing URLs into clean URLs. http://httpd.apache.org/docs/current/mod/mod_rewrite.html has the full documentation on mod_rewrite, but you'll probably learn faster from examples. jValdron just posted a decent one. And this answer is quickly becoming a mishmash of faster answers. There is so much documentation for rewrites and handling them with PHP that a question this broad isn't really worth answering again.
If you have a very large site with a lot of php pages that take query strings, you're going to have to plan on either writing a ton of rewrites and making sure your links don't break or a single rewrite to a script that will handle redirecting to the right pages.
If you are making a new site, I'd go with rewriting your URLs to a single script that will get the content according to the URL.
Here is a famous example from Drupal's .htaccess file.
Then
QSA
means query string append and theL
before it means LastStop the rewriting process immediately and don't apply any more rules.
Then index.php can access the URL with $_GET['q'].