使用 PHP 创建 SEO 永久链接,无需 .htaccess

发布于 2024-10-17 15:35:17 字数 237 浏览 1 评论 0原文

目前,我的页面 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

稀香 2024-10-24 15:35:17

我如何使用 php 而不是 .htaccess 来完成此任务..

你不能。您需要告诉 Web 服务器如何处理实际不存在的 URL。在 Apache 中,这是在中央配置或 .htaccess 文件中完成的。

如果您的服务器已经启用了 AccepPathInfo On,您可以尝试使用类似这样的 URL

http://ourdomain.com/index.php/articles/blah-blah-blah

来重定向到 index.php 并添加 articles/blah-blah-blah< /code> 在 $_SERVER["PATH_INFO"] 变量中。这种方法被称为“穷人的 URL 重写”,因为您无法删除 URL 中的 index.php 部分。如果打开上述设置(我认为这是默认设置),您也许可以在不使用 .htaccess 文件的情况下执行此操作。

How can i accomplish this using php but not with .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 like

http://ourdomain.com/index.php/articles/blah-blah-blah

which will redirect to index.php and have articles/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 the index.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.

弥繁 2024-10-24 15:35:17

如果您有权访问服务器配置,则无需 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.

灰色世界里的红玫瑰 2024-10-24 15:35:17

我认为如果不改变 .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

怎言笑 2024-10-24 15:35:17

可以在 PHP 中完成此操作,而无需修改 .htaccess

只需在 index.phpdefault.php 中编写以下代码即可。

<?php
if (isset($_GET['permalink'])) {
    header('Location: '.urlencode($_GET['permalink']));
}
?>

时:

http://ourdomain.com/articles/?permalink=blah-blah-blah

它可以工作,因为当您键入以下URL 未指定文件名。
因此,服务器会检查指定目录中是否存在“index”或“default”文件。

考虑文件index.php存在,因此服务器将调用:

http://ourdomain.com/articles/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

<?php
if (isset($_GET['permalink'])) {
    header('Location: '.urlencode($_GET['permalink']));
}
?>

It works because when you type following URL:

http://ourdomain.com/articles/?permalink=blah-blah-blah

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:

http://ourdomain.com/articles/index.php

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文