首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在.htaccess中在Wordpress生成的行之间插入行

问在.htaccess中在Wordpress生成的行之间插入行
EN

Stack Overflow用户
提问于 2015-01-05 06:01:11
回答 1查看 640关注 0票数 0

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

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

代码语言:javascript
复制
# 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

如下所示:

代码语言:javascript
复制
# 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生成的行的上方和下方添加了行。

EN

回答 1

Stack Overflow用户

发布于 2015-01-05 06:18:17

您是否可以使用Generate Rewrite Rules操作打开.htaccess文件并插入额外的内容?

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

下面是一些伪代码:

代码语言:javascript
复制
// 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

这可能有点麻烦,但在我看来,这比参与实际的内置重写函数要好得多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27770831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档