Wordpress - 在.htaccess中的wordpress生成行之间插入行

时间:2015-01-04 22:01:11

标签: wordpress .htaccess mod-rewrite

我需要在.htaccess文件中生成wordpress生成的行之间动态添加几行。

这是.htaccess文件在wordpress生成时的样子:

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

# END WordPress

我想在两者之间添加我的线条 RewriteRule ^ index.php $ - [L]

RewriteCond%{REQUEST_FILENAME}!-f

像这样:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# BEGIN THIS DL-FILE.PHP ADDITION
RewriteCond %{REQUEST_URI} ^.*content/uploads/PROTECTEDFOLDER/.*
RewriteRule ^wp-content/uploads/(PROTECTEDFOLDER/.*)$ wordpress/dl-file.php?file=$1 [QSA,L]
# END THIS DL-FILE.PHP ADDITION

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

我尝试使用mod_rewrite_rules filter添加行,但只设法在wordpress生成的行上方和下方添加行。

1 个答案:

答案 0 :(得分:0)

您是否可以使用“生成重写规则”操作打开.htaccess文件并插入额外的位?

http://codex.wordpress.org/Plugin_API/Action_Reference/generate_rewrite_rules

这是一些伪代码:

// On the generate_rewrite_rules hook...

// Grab the htaccess file
$htaccess = file_get_contents(get_home_path() . '.htaccess');
// find where you want to insert
$character_position = strpos($htaccess, "RewriteCond %{REQUEST_FILENAME} !-f"); 
$new_htaccess = substr_replace($htaccess, "#BEGIN ...", $character_position, 0);
// Save the file... etc

可能有点笨拙,但在我看来,比参与实际内置的重写功能要好得多。