简体   繁体   English

使用 PHP 更改 .htaccess - 删除重写规则

[英]Altering .htaccess with PHP - removing a rewrite rule

I am using PHP to remove/add static pages once a page has been deleted, I want to be able to remove it from the .htaccess, however I've tried this, but it throws an error:一旦页面被删除,我正在使用 PHP 来删除/添加静态页面,我希望能够从 .htaccess 中删除它,但是我已经尝试过这个,但它抛出了一个错误:

Warning : preg_replace() [function.preg-replace]: Unknown modifier '' in ...警告:preg_replace() [function.preg-replace]:未知修饰符 '' in ...

The code:编码:

$page_name = $row['page_name']; // Example:  help
preg_replace('/RewriteRule ' . preg_quote('^' . $page_name . '/?$ page.php?mode=') . '.*/i', '', $htaccess);

This is an example of what it should fully remove:这是它应该完全删除的示例:

RewriteRule ^help/?$ page.php?mode=help

You have to escape the expression delimiter by passing it to preg_quote as the second argument.您必须通过将表达式分隔符作为第二个参数传递给preg_quote来对其进行preg_quote

preg_replace('/RewriteRule ' . preg_quote('^' . $page_name . '/?$ page.php?mode=', '/') . '.*/i', '', $htaccess);

Or else your / won't be escaped.否则你的 / 将不会被转义。 As stated in the documentation "the special regular expression characters are: . \\ + * ? [ ^ ] $ ( ) { } = ! < > | : -"如文档中所述“特殊的正则表达式字符是:. \\ + * ? [ ^ ] $ ( ) { } = ! < > | :-”

USe like this像这样使用

preg_replace ( "~~msi", "pattern to replace"). preg_replace(“~~msi”,“要替换的模式”)。 Also - good practive is analise by line's not - change in all text a time!!!另外 - 好的做法是逐行分析 - 一次更改所有文本!!!

so所以

foreach ( file(.htaccess) as $line) foreach ( file(.htaccess) as $line)
{ {

and replace in each line, }并在每一行中替换,}

than output all, store copy of old .htaccess ...比输出所有,存储旧 .htaccess 的副本......

,Arsen ,阿森

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM