使用 PHP 创建 SEO 永久链接,无需 .htaccess
目前,我的页面 URL 看起来是这样的:
http://ourdomain.com/articles/?permalink=blah-blah-blah
我想将它们转换为:
http://ourdomain.com/articles/blah-blah-blah
如何使用 PHP 而不是 .htaccess 来完成此操作?
Currently, my page URLs look this this:
http://ourdomain.com/articles/?permalink=blah-blah-blah
I want to convert these to:
http://ourdomain.com/articles/blah-blah-blah
How can I accomplish this using PHP but not with .htaccess?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不能。您需要告诉 Web 服务器如何处理实际不存在的 URL。在 Apache 中,这是在中央配置或 .htaccess 文件中完成的。
如果您的服务器已经启用了
AccepPathInfo On
,您可以尝试使用类似这样的 URL来重定向到
index.php
并添加articles/blah-blah-blah< /code> 在
$_SERVER["PATH_INFO"]
变量中。这种方法被称为“穷人的 URL 重写”,因为您无法删除 URL 中的index.php
部分。如果打开上述设置(我认为这是默认设置),您也许可以在不使用 .htaccess 文件的情况下执行此操作。You can't. You will need to tell the web server how to deal with URLs that don't physically exist. In Apache, that is done in the central configuration or in a .htaccess file.
If your server already happens to have
AccepPathInfo On
, you can try having URLs likewhich will redirect to
index.php
and havearticles/blah-blah-blah
in the$_SERVER["PATH_INFO"]
variable. This method is known as "poor man's URL rewriting" because you can't get rid of theindex.php
part in the URL. If the mentioned setting is turned on (I think it is by default), you may be able to do this without using a .htaccess file.如果您有权访问服务器配置,则无需 mod_rewrite 即可实现此目的。假设您使用的是 Apache,您需要做的第一件事是在文档根目录上打开 MultiViews 选项(即添加
Options MultiViews
)。现在将 /articles/index.php 复制到 /articles.php (因此将脚本放在文档根目录中并重命名),并调整脚本,使其读取$_SERVER["PATH_INFO"]
到获取正确的页面(这当然依赖于 AcceptPathInfo 打开)。当您提供 /articles/blah-blah URL 时,MultiViews 将确保调用articles.php 脚本。
You can achieve this without mod_rewrite if you have access to the server configuration. Assuming you're using Apache, the first thing you would need to do is turn the MultiViews option on on your document root (ie. add
Options MultiViews
). Now copy your /articles/index.php to /articles.php (so put the script in your document root and rename it), and adapt your script so it reads$_SERVER["PATH_INFO"]
to fetch the correct page (this of course relies on having AcceptPathInfo On).MultiViews will make sure that the articles.php script is called when you provide a /articles/blah-blah URL.
我认为如果不改变 .htaccess 就可以轻松做到这一点。您肯定需要使用 mod_rewrite。请参阅此处的答案以获取更多信息:
特殊个人资料页面链接,例如 www.domain.com /用户名
I don't think you can easily do it without altering .htaccess. You'll most definitely need to use mod_rewrite. See the answers here for more info:
Special profile page link like www.domain.com/username
可以在 PHP 中完成此操作,而无需修改 .htaccess
只需在 index.php 或 default.php 中编写以下代码即可。
时:
它可以工作,因为当您键入以下URL 未指定文件名。
因此,服务器会检查指定目录中是否存在“index”或“default”文件。
考虑文件index.php存在,因此服务器将调用:
with blah-blah-blah in GET variable permalink
PHP代码检查是否permalink存在 GET 变量,并使用 header() 方法进行重定向。
编辑:添加 urlencode() 进行输入验证
It is possible to do it in PHP, without modifying .htaccess
Just write following code in either index.php or default.php
It works because when you type following URL:
The filename is not specified.
So, the server looks whether "index" or "default" file is present in the specified directory.
Consider file index.php is present, so server will call:
with blah-blah-blah in GET variable permalink
The PHP code checks if permalink GET variable is present, and redirects using header() method.
EDIT: added urlencode() to do input validation