简体   繁体   English

查找所有.htaccess文件,然后用PHP脚本替换内容

[英]Find all .htaccess files and replace content with PHP script

I need to create a script that will find all my .htaccess files on my server and replace their content with the new content I need in order for my pages to be SEO friendly. 我需要创建一个脚本,该脚本将在服务器上找到我的所有.htaccess文件,并将其内容替换为所需的新内容,以使页面对SEO友好。

So far I've come across a few scripts that will find all the .htaccess files but I need to be able to open, replace all content with new and save with the proper permissions. 到目前为止,我遇到了一些脚本,这些脚本可以找到所有.htaccess文件,但我需要能够打开,使用new替换所有内容并使用适当的权限进行保存。

Can anyone help me with the following code to add the extra functionality I need? 谁能用以下代码帮助我添加所需的其他功能?

<?php

function searchDir($dir) {
   $dhandle = opendir($dir);
   if ($dhandle) {
      // loop through it
      while (false !== ($fname = readdir($dhandle))) {
         // if the element is a directory, and does not start with a '.' or '..'
         // we call searchDir function recursively passing this element as a parameter
         if (is_dir( "{$dir}/{$fname}" )) {
            if (($fname != '.') && ($fname != '..')) {
               echo "Searching Files in the Directory: {$dir}/{$fname} <br />";
               searchDir("$dir/$fname");
            }
         // if the element is an .htaccess file then replace content
         } else {
            if($fname == ".htaccess")
            {
               echo "Replacing content of file ".$dir/$fname."<br />";
               // I need the code for editing the files here.
            }
         }
      }
      closedir($dhandle);
    }
}

searchDir(".");


?>

Modify your else loop with this 以此修改您的else循环

else {
  if($fname == ".htaccess")
  {
   echo "Replacing content of file ".$dir/$fname."<br />";
   // I need the code for editing the files here.
   $htaccess_content = " Your htaccess string " ;  // you can do this assignment at the top
   file_put_contents("{$dir}/{$fname}",$htaccess_content) ;
  }
}

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

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