php - Add htaccess rules with insert_with_markers at beginning of htaccess

From my custom plugin, on activation, i want add some rules into htaccess but at beginning of the file.insert_with_marke

From my custom plugin, on activation, i want add some rules into htaccess but at beginning of the file.

insert_with_markers add the rule at the end of htaccess, eg.:

$lines = array();
$lines[] = '# MY RULES';
return insert_with_markers(ABSPATH . '.htaccess', 'TEST', $lines);

result

# 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
# BEGIN TEST
# MY RULES
# END TEST

expected

# BEGIN TEST
# MY RULES
# END TEST
# 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

how add my rules at beginning of htaccess?

From my custom plugin, on activation, i want add some rules into htaccess but at beginning of the file.

insert_with_markers add the rule at the end of htaccess, eg.:

$lines = array();
$lines[] = '# MY RULES';
return insert_with_markers(ABSPATH . '.htaccess', 'TEST', $lines);

result

# 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
# BEGIN TEST
# MY RULES
# END TEST

expected

# BEGIN TEST
# MY RULES
# END TEST
# 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

how add my rules at beginning of htaccess?

Share Improve this question asked Sep 29, 2019 at 9:16 Simone NigroSimone Nigro 1658 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Using insert_with_markers() function you can insert your rules only between WordPress comments e.i. # BEGIN WordPress and # END WordPress.

So, you should use plain PHP to achieve your aim.

  1. Create your rules
  2. Read existing .htaccess and concatenate your and existing rules
  3. Write the file

Here is a dirty example:

<?php
$str  = "# BEGIN  TEST\n# MY RULES\n# END TEST\n";
$str .= file_get_contents( ABSPATH . '.htaccess' );
file_put_contents( ABSPATH . '.htaccess', $str );

Or use FILE_APPEND flag with the file_put_contents().

Remember!

It's a very bad idea to allow PHP to alter your .htaccess. One infected file is enough to break your server.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745121401a4612434.html

相关推荐

  • php - Add htaccess rules with insert_with_markers at beginning of htaccess

    From my custom plugin, on activation, i want add some rules into htaccess but at beginning of the file.insert_with_marke

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信