简单的 WordPress 重写规则

发布于 2024-12-14 07:44:41 字数 1084 浏览 0 评论 0原文

我面临着 wordpress / htaccess 问题。这应该非常简单,但它不起作用,我很沮丧。

我的网站以这种方式处理永久链接: http://www.my-site.com/articles/category/title

现在,有一个我称为“特写”的类别,我不希望显示“文章”的 URL。因此,我想

访问http://www.my-site.com/articles/closeup/childCategory/title

使用这样的 URL

中的内容http://www.my- site.com/closeup/childCategory/

我已经编写了一个页面,用于处理在同一页面上显示所有文章的子类别。

所以我写了一个htaccess重写规则如下:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^closeup/(.*) articles/closeup/$1 [L]
</IfModule>

我在一个允许我测试我的htaccess规则的网站上在线测试了htaccess,并且它在那里工作得很好,但在我的博客上却不行。有什么建议吗?

所以整个htaccess是:

# BEGIN BS

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^closeup/(.*) articles/closeup/$1
</IfModule>

# END BS

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

I'm facing a wordpress / htaccess problem. This should be really simple, but it's not working and I'm getting frustrated.

My site handles permalinks this way: http://www.my-site.com/articles/category/title

Now, there is a category I called 'closeup' for which I don't want the URL to show 'article'. So I would like to access content from

http://www.my-site.com/articles/closeup/childCategory/title

with an URL like this

http://www.my-site.com/closeup/childCategory/

I already wrote a page that handles the child category showing all articles on the same page.

So I wrote an htaccess rewrite rule as follows:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^closeup/(.*) articles/closeup/$1 [L]
</IfModule>

I tested the htaccess online on a website which allowed me to test my htaccess rules, and it works fine there, but not on my blog. Any tip?

So the whole htaccess is:

# BEGIN BS

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^closeup/(.*) articles/closeup/$1
</IfModule>

# END BS

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

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

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

发布评论

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

评论(2

亚希 2024-12-21 07:44:41

如果您将其添加到 # Begin Wordpress 块上方,它应该可以解决问题:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^closeup/(.*) index.php/articles/closeup/$1 [L]
</IfModule>

背景:

ReweriteRule 正在执行您所期望的操作,这是 WordPress 找不到该页面。要根据永久链接查找帖子,Wordpress 使用

$_SERVER['PATH_INFO']

$_SERVER['REQUEST_URI']

在您的情况下,即使在 RewrtieRule 之后,URL 也会显示为

http://www.my-site.com/closeup/childCategory/

WordPress 使用 $_SERVER['REQUEST_URI'] (closeup/childCategory/) 来解析永久链接并返回 404。

如果您使用 RewriteRule 在 index.php 之后传递所需的路径,

http://www.my-site.com/index.php?/articles/closeup/childCategory/

Wordpress 将找到您的路径信息(因为它尝试首先是 $_SERVER['PATH_INFO']),因此永久链接将为 articles/closeup/childCategory/

If you add this above your # Begin Wordpress block, it should solve the problem:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^closeup/(.*) index.php/articles/closeup/$1 [L]
</IfModule>

Background:

ReweriteRule is doing what you expect, it's Wordpress that can't find the page. To find the post based on the permalink, Wordpress uses either

$_SERVER['PATH_INFO']

or

$_SERVER['REQUEST_URI']

In your case, even after the RewrtieRule the URL appears as

http://www.my-site.com/closeup/childCategory/

Wordpress uses $_SERVER['REQUEST_URI'] (closeup/childCategory/) to parse the permalink and returns a 404.

If you use RewriteRule to pass the desired path after index.php

http://www.my-site.com/index.php?/articles/closeup/childCategory/

Wordpress will find your path info (because it tries $_SERVER['PATH_INFO'] first) so the permalink will be articles/closeup/childCategory/

终难愈 2024-12-21 07:44:41

为什么不使用 WordPress 的自定义分类功能呢?创建“特写”页面/帖子类型。

Why not use Wordpress's custom taxonomy capability instead? Create a page/post type of "closeup."

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