简体   繁体   English

WordPress + mod_rewrite

[英]WordPress + mod_rewrite

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. 我将Wordpress用作CMS,首页的大部分内容都是静态的,而博客的实际部分已移至使用自定义页面模板的新页面,该模板的基本作用就像是标准页面的首页一样。 WordPress安装。 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。

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). 到目前为止,我已经将Wordpress配置为显示正确的URL(domain.com/blog/page/2而不是domain.com/page/2),并且我还通过它显示了特定的博客帖子页面。 URL参数(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. 因此,基本上我快完成了,只需要将domain.com/blog/page/N映射到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 如果这只是一个简单的旧静态网站,这将是一件容易的事,但是Wordpress有它自己的URL处理,我相信这可能会干扰它-更不用说“ blog”不是实际的URL,因此它需要通过规则两次,一次用于映射博客分页,一次再次使Wordpress可以按常规处理生成的URL……我想我可能需要对重写规则使用N或NS标志,但我只是不这样做知道我将如何使用它们

The rule I've come up with: 我想出的规则:

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

Default Wordpress .htaccess: 默认的Wordpress .htaccess:

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

As per some suggestions, I've gotten this for the .htaccess, but it still doesn't recognize it. 根据一些建议,我已经为.htaccess找到了它,但是它仍然无法识别它。

<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>

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. 而且我相当确定通过URL参数更改页面确实可以-我手动更改URL中的数字,实际上确实可以更改页面。 The web server is LiteSpeed, if that makes any difference Web服务器为LiteSpeed,如果有区别的话

I figured it out eventually (a few days after -- I just forgot about this until now). 我终于想通了(几天后-直到现在我都忘记了)。 First, make sure you have Wordpress updated. 首先,请确保您已更新Wordpress。 At the time I was using 2.5 or 2.6 but for some reason it didn't work until I updated. 当时我使用的是2.5或2.6,但由于某种原因,直到我进行了更新,它才起作用。

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. 单独保留“ Main Index”(index.php)模板-该模板将用于博客索引页面,因此它需要WP循环来列出您的博客文章。

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"). 一个用于您的主页(简称为“ blog”),另一个用于您的博客(简称为“ 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. 对于帖子页面,选择您创建的“博客”页面。

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. 我最终成为过时的WP安装。 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. 如果您安装了站点地图模块,请确保排除“主页”页面,以免蜘蛛程序在example.com/home上找到主页的副本。 You can also put it in your robots.txt to be sure. 您也可以确保将其放入robots.txt中。

Once you're done, the root (example.com/) of your domain (assuming you have WP installed there) will point to your "home" page. 完成后,域的根目录(example.com /)(假设已在其中安装WP)将指向“主页”页面。 Your blog page (example.com/blog) will list your posts. 您的博客页面(example.com/blog)将列出您的帖子。 Pagination will work as expected (example.com/blog/page/2 etc.). 分页将按预期进行(example.com/blog/page/2等)。

Try the following 尝试以下

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

Or try 或尝试

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

The NC makes it case insensitive, the N make it reapply the rules, and the L stops the rule processing. NC使其不区分大小写,N使其重新应用规则,L停止规则处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM