最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

htaccess - Why does a directive only work when it is included inside WordPress directives?

programmeradmin1浏览0评论

The directive below is intended to block access to the thank you page. It appears below the WordPress directives. It does not work as intended in that the page remains accessible and does not display a 403 error

RewriteCond %{THE_REQUEST} ^(.*)?thanks?(/)$ [NC]
RewriteRule ^(.*)$ - [F,L]

When the directive is included as part of the WordPress directives, it works as intended.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} ^(.*)?thanks?(/)$ [NC]
RewriteRule ^(.*)$ - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Why does the first block of directives not work and why does it only work when it's part of the WordPress directives?

The directive below is intended to block access to the thank you page. It appears below the WordPress directives. It does not work as intended in that the page remains accessible and does not display a 403 error

RewriteCond %{THE_REQUEST} ^(.*)?thanks?(/)$ [NC]
RewriteRule ^(.*)$ - [F,L]

When the directive is included as part of the WordPress directives, it works as intended.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} ^(.*)?thanks?(/)$ [NC]
RewriteRule ^(.*)$ - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Why does the first block of directives not work and why does it only work when it's part of the WordPress directives?

Share Improve this question asked Sep 17, 2019 at 19:09 MotivatedMotivated 2452 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

When the directive is included as part of the WordPress directives

It simply needs to go before the WordPress front-controller. In fact, you should not include this as "part of the WordPress directives" as WP itself tries to maintain this block of code and could override your custom directives in a future update.

You should put this custom directive directive before the # BEGIN WordPress block.

It does not work if you put the directives "below the WordPress directives" because it is simply never processed. The preceding WP directives route all requests (for non-existent files/directories) to /index.php - after which processing stops and any mod_rewrite directives that follow are bypassed.

It would only work (with directives after the WP front-controller) if the "thank you" request mapped directly to a physical file on the filesystem - in which case the request is not rewritten to /index.php and processing is allowed to continue through the file.


Aside:

RewriteCond %{REQUEST_URI} ^(.*)?thanks?(/)$ [NC]
RewriteRule ^(.*)$ - [F,L]

This can be simplified to a single directive:

RewriteRule thanks?/$ - [NC,F]

The L (last) flag is not required when the F flag is used; it is implied.

发布评论

评论列表(0)

  1. 暂无评论