惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

Comments for Jeremy Carlson

Pay attention. – Jeremy Carlson Can I use type in an image? – Jeremy Carlson Extract Source Images from a PDF in Photoshop – Jeremy Carlson
Excluding a directory from WordPress – Jeremy Carlson
Jeremy · 2017-02-04 · via Comments for Jeremy Carlson

I’m working on a site, and we have been storing archive copies of our newsletter on our site like this:

http://example.com/newsletter/2015-05/

The site itself has been a homegrown PHP solution which I’m finally, after years, converting to WordPress. One challenge is this folder structure. I don’t really want to mess with it. But I need http://example.com to run with WordPress. So I need to exclude the newsletter directory (folder, whatever).

I looked around and found a great tip on excluding a directory which shows just how to write the proper code into my .htaccess file, like this:

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

The salient line that’s added is saying the REQUEST_URI must NOT be either ‘mydir’ or ‘mydir/whatever’. It’s great, works a treat.

But there’s one small problem.

If you change your permalinks, your rewrite rules are flushed, and the .htaccess file is overwritten. Now, you could just make sure your .htaccess file is not writable, but where’s the fun in that? Instead I want to tap into WordPress’s WP_Rewrite API and make sure that, should we change the file, this rule will be preserved.

Luckily the API has lots of hooks so we can manipulate things. I initially looked using the add_rewrite_rule function to add to the rewrite_rules array—since WordPress at one point has an array of all the rewrite rules that seemed handy. But then I noticed that the action to hook into that only seems to be working for rewrite RULES, not rewrite CONDITIONS.

Someone had already asked if WordPress had a function to generate a rewrite condition on StackExchange. The answer given was very helpful, leading me to use the mod_rewrite_rules hook to mess with the entire string of content just before it gets written to .htaccess.

First I need to define my new rule, like this:

// Create our condition.
$exclude_newsletter_cond = PHP_EOL . 'RewriteCond %{REQUEST_URI} !^/(newsletter/.*)$';

Then I’m going to insert this condition just after the line with RewriteBase—that’s a unique point that comes somewhere in the midst of the rules. This means the condition will get inserted BEFORE the first RewriteRule. But it works just fine. (YMMV)

I’m going to do a preg_replace to search for ‘RewriteBase.+’ and replace it that with itself, plus the string I created above. My search looks like this—remember you need the forward slashes in your search string for preg_replace:

$search = '/RewriteBase.+/';

And then we just replace it. You can see this in the full code example below.

/**
* We need to parse the rewrite rules string, and insert something
* INSIDE it. If this were a rewrite RULE, then we could insert it
* into the array that gets prepped before getting converted into a
* string. If you want to do that, may want to check out the Codex:
* https://codex.wordpress.org/Class_Reference/WP_Rewrite
* (Note: this would be a different hook , too)
*
* In our case, since we are creating a CONDITION, we need to muscle
* our way into the string.
*/
function my_custom_htaccess( $rules ){

// Create our condition.
$exclude_newsletter_cond = PHP_EOL . 'RewriteCond %{REQUEST_URI} !^/(newsletter/.*)$';

// Look for a place to insert it.
// We are going to put this just after the RewriteBase line.
// This is not in the same place as all the other RewriteCond statements,
// But it seems to work.
$search = '/RewriteBase.+/';

// Replace w/ found string, plus condition
$replace = '$0' . $exclude_newsletter_cond;

// Rock and roll.
$rules = preg_replace($search, $replace, $rules);

return $rules;
}
add_filter('mod_rewrite_rules', 'my_custom_htaccess');

This produces the following set of WordPress rules:

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

So yes, the RewriteCond does come before the first RewriteRule, but it worked locally. I’ll update this post if I find other issues.

If you found this, and it was helpful, let me know!