开发者

Altering .htaccess with PHP - removing a rewrite rule

开发者 https://www.devze.com 2023-03-03 11:04 出处:网络
I am using PHP to remove/ad开发者_C百科d 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:

I am using PHP to remove/ad开发者_C百科d 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


You have to escape the expression delimiter by passing it to preg_quote as the second argument.

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"). 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

0

精彩评论

暂无评论...
验证码 换一张
取 消