WordPress + mod_rewrite

I use Wordpress as a CMS, where the main page is mostly static, and the actual part of the blog has been moved to a new page that uses a custom page template, which basically acts as if it were the first page on a standard Wordpress installation. It works very well, but it still has some bugs. The main thing is that pagination will no longer work; Wordpress will not recognize the generated URLs of the pages.

So far, I have configured Wordpress to display the correct URLs (domain.com/blog/page/2 instead of domain.com/page/2), and I also got it to display a specific message blog page via the URL parameter (domain.com / blog? page = N). Therefore, basically, I'm almost ready, I just need to map domain.com/blog/page/N to domain.com/blog?page=N. It would be a simple task, if it was just an old static site, but Wordpress has its own URL processing, which, I think, can interfere with it - not to mention a “blog”, this is not a real URL, so it you need to go through the rules twice, once to display the pagination on the blog, and once again so that Wordpress can handle the generated URL, as it would be regularly ... I thinkI may need to use the N or NS flags for rewriting rules, but I just don’t know how I will use them

The rule I came up with:

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

By default, Wordpress.htaccess:

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

According to some suggestions, I got this for .htaccess, but it still does not 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>

And I'm sure that changing the page using the URL parameter really works - I manually change the number in the URL and actually change the page. LiteSpeed ​​web server, if that matters

0
source share
3 answers

I realized this in the end (after a few days - I just forgot about it until now). First, make sure you have Wordpress. I used 2.5 or 2.6 at the time, but for some reason this did not work until I updated.

. , . " " (index.php) - , WP, .

. ( "" ), ( "" ). , . - " " .

"" " ". "". "blog".

. , , , . , WP install. sitemap, , "" , example.com/home. robots.txt.

, root (example.com/) ( , WP) . (example.com/blog) . Pagination (example.com/blog/page/2 ..).

+1

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

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>

NC makes the case insensitive, N makes it reapplicable to rules, and L stops rule processing.

0
source

Source: https://habr.com/ru/post/1706293/


All Articles