WordPress + mod_rewrite [英] WordPress + mod_rewrite

查看:92
本文介绍了WordPress + mod_rewrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Wordpress用作CMS,其中首页大部分是静态的,而其实际博客部分已移至使用自定义页面模板的新页面,该模板的基本作用就像是首页在标准的Wordpress安装上.它工作得很好,但仍然存在一些错误.最主要的是分页将不再起作用,Wordpress无法识别所生成的页面URL.

I use Wordpress as a CMS, where the front page is static for the most part and the actual blog part of it has been moved to a new page that uses a custom page template that basically acts as if it were the front page on a standard Wordpress install. It works pretty well but it still has a few bugs. The main one is that pagination will no longer work, Wordpress won't recognize the page URLs that are generated.

到目前为止,我已经配置了Wordpress以显示正确的URL(domain.com/blog/page/2而不是domain.com/page/2),并且我还获得了它来显示博客的特定页面通过URL参数(domain.com/blog?page=N)发布.所以基本上我快完成了,我只需要将domain.com/blog/page/N映射到domain.com/blog?page=N.如果这只是一个简单的旧静态网站,这将是一件容易的事,但是Wordpress拥有它自己的URL处理,我相信这可能会干扰它-更不用说"blog"不是实际的URL,因此它需要通过规则两次,一次用于映射博客分页,一次再次使Wordpress可以按常规处理生成的URL……我认为我可能需要对重写规则使用N或NS标志,但我只是不需要知道我会怎么用

So far I've configured Wordpress to display the correct URLs (domain.com/blog/page/2 instead of domain.com/page/2), and I've also gotten it to display a specific page of blog posts via a URL parameter (domain.com/blog?page=N). So basically I'm almost done, I just need to map domain.com/blog/page/N to domain.com/blog?page=N. This would be an easy task if it were just a plain old static site, but Wordpress has it's own URL handling that I believe may be interfering with it - not to mention "blog" isn't an actual URL so it needs to go through the rules twice, once for mapping the blog pagination, and once again so Wordpress can handle the generated URL as it would regularly... I think I may need to use the N or NS flags for the rewrite rules but I just don't know how I would use them

我想出的规则:

RewriteRule ^/blog/page/([0-9]+)$ /blog?page=$1

默认Wordpress .htaccess:

Default Wordpress .htaccess:

RewriteEngine On  
RewriteBase /  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule . /index.php [L]  

根据一些建议,我已经为.htaccess找到了它,但它仍然无法识别它.

As per some suggestions, I've gotten this for the .htaccess, but it still doesn't recognize it.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule /blog/page/([0-9]+)$ /blog?page=$1 [N,QSA]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

我敢肯定,通过URL参数更改页面确实可行-我手动更改了URL中的数字,实际上它确实更改了页面. Web服务器是LiteSpeed,如果有区别的话

And I'm fairly certain changing the page via the URL parameter does work - I change the number in the URL manually and it does in fact change the page. The web server is LiteSpeed, if that makes any difference

推荐答案

我最终弄清楚了(几天后-直到现在我才忘记了这一点).首先,请确保您有Wordpress更新.当时我使用的是2.5或2.6,但由于某种原因,直到我进行更新后它才起作用.

I figured it out eventually (a few days after -- I just forgot about this until now). First, make sure you have Wordpress updated. At the time I was using 2.5 or 2.6 but for some reason it didn't work until I updated.

首先,创建一个自定义模板.您将在主页上使用此模板,因此请自定义它以及所有爵士乐.单独保留主索引"(index.php)模板-该模板将用于博客索引页面,因此它需要WP循环来列出您的博客文章.

First, create a custom template. You will use this template for your homepage so customize it and all that jazz. Leave the "Main Index" (index.php) template alone -- that template will be used for the blog index page so it needs the WP loop to list your blog posts.

现在,转到创建两个页面.一个用于您的主页(简称为"blog"),另一个用于您的博客(简称为"blog").在主页"页面上,将该模板设置为您先前创建的主页模板.在博客页面上,将其保留在默认模板上-默认情况下,它将使用主索引"模板.

Now, go and create two pages. One for your home page (let's call it "home") and another for your blog (let's call it "blog"). On the "home" page, set the the template to the homepage template you created previously. On the blog page, leave it on the default template -- it will use the "Main Index" template by default.

现在,进入阅读"设置并将其更改为静态页面".对于首页,选择先前创建的主页"页面.对于帖子页面,选择您创建的博客"页面.

Now, go into your "Reading" settings and change it to "A static page." For the front page, select the "home" page you created previously. For the posts page, select the "blog" page you created.

应该的.我实际上已经事先尝试过了,但是它没有用,所以我尝试使用重定向规则,但无济于事.我最终成为过时的WP安装.如果您安装了站点地图模块,请确保排除主页"页面,以免蜘蛛程序在example.com/home上找到主页的副本.您也可以确保将其放入robots.txt中.

That should be it. I had actually tried this beforehand but it did not work so I tried using redirect rules, to no avail. I ended up being an out of date WP install. If you have a sitemap mod installed, make sure you exclude the "home" page so the spiders don't pick up a duplicate of your home page at example.com/home. You can also put it in your robots.txt to be sure.

完成后,域的根目录(example.com/)(假设已在其中安装WP)将指向主页"页面.您的博客页面(example.com/blog)将列出您的帖子.分页将按预期进行(example.com/blog/page/2等).

Once you're done, the root (example.com/) of your domain (assuming you have WP installed there) will point to your "home" page. Your blog page (example.com/blog) will list your posts. Pagination will work as expected (example.com/blog/page/2 etc.).

这篇关于WordPress + mod_rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆