使用 PHP 更改 .htaccess - 删除重写规则
我正在使用 PHP 在删除页面后删除/添加静态页面,我希望能够从 .htaccess 中删除它,但是我已经尝试过这样做,但它会引发错误:
警告:preg_replace() [function.preg-replace]:未知修饰符“” in ...
代码:
$page_name = $row['page_name']; // Example: help
preg_replace('/RewriteRule ' . preg_quote('^' . $page_name . '/?$ page.php?mode=') . '.*/i', '', $htaccess);
这是应该完全删除的内容的示例:
RewriteRule ^help/?$ page.php?mode=help
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:
Warning: preg_replace() [function.preg-replace]: Unknown modifier '' 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将表达式分隔符传递给
preg_quote
作为第二个参数来转义。否则你的 / 将不会被转义。正如文档中所述“特殊正则表达式字符为:. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -”
You have to escape the expression delimiter by passing it to
preg_quote
as the second argument.Or else your / won't be escaped. As stated in the documentation "the special regular expression characters are: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -"
像这样使用
preg_replace ("~~msi", "pattern to replacement")。
另外 - 好的做法是按行进行分析,而不是 - 一次更改所有文本!
所以
foreach ( file(.htaccess) as $line)
{
并在每一行中替换,
}
比输出全部,存储旧 .htaccess 的副本...
,Arsen
USe like this
preg_replace ( "~~msi", "pattern to replace").
Also - good practive is analise by line's not - change in all text a time!!!
so
foreach ( file(.htaccess) as $line)
{
and replace in each line,
}
than output all, store copy of old .htaccess ...
,Arsen