mod_rewrite 用于友好的 URL

发布于 2025-01-02 13:13:13 字数 651 浏览 1 评论 0原文

我正在尝试将永久链接实现到我的内容管理系统中,但我似乎遇到了 mod_rewrite 的问题。我有一个 PHP 文件 (single.php),它将根据传递给它的永久链接名称显示单个帖子。即post.php?permalink=name-of-post-here

以下是我设置的规则:

RewriteRule ^([0-9]{4})/([a-z]+)?$ $1/ [R]
RewriteRule ^([0-9]{4})/([a-z]+)?$ post.php?permalink=$1

另外,如何获取日期/年份(即 2012 年),并将其分配为 PHP 文件的参数。 (即 post.php?year=2012?permalink=name-of-post-here)所有这些都可以使用 mod_rewrite 来完成吗?

非常感谢。

想要将

http://www.website.com/post.php?year=2012&permalink=post-name-here

更改为

http://www.website.com/2012 /帖子名称-此处

I'm trying to implement Permalinks into my content management system and I seem to be stuck at a problem with mod_rewrite. I have a PHP file (single.php) which will display a single post based on the Permalink name that gets passed through to it. i.e. post.php?permalink=name-of-post-here.

Here are the rules I've set up:

RewriteRule ^([0-9]{4})/([a-z]+)?$ $1/ [R]
RewriteRule ^([0-9]{4})/([a-z]+)?$ post.php?permalink=$1

Also, how do I get the date/year (i.e. 2012), and assign that as a parameter for the PHP file. (i.e. post.php?year=2012?permalink=name-of-post-here) & can all of this be done using mod_rewrite?

Many Thanks.

Want to change

http://www.website.com/post.php?year=2012&permalink=post-name-here

to

http://www.website.com/2012/post-name-here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一口甜 2025-01-09 13:13:13

您应该使用以下代码:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^([0-9]{4})/(.*)$ post.php?year=$1&permalink=$2 [L,R,NC,QSA]

如果您不希望外部重定向(在浏览器中更改 URL),请删除 R 标志。

You should use this code:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^([0-9]{4})/(.*)$ post.php?year=$1&permalink=$2 [L,R,NC,QSA]

If you don't want external redirect (change URL in browser) then remove R flag.

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