htaccess,在浏览器中搜索和替换关键字

时间:2011-12-28 22:58:44

标签: php apache .htaccess search replace

我需要在100,000个文件中重写一个关键字(旧域名),我无法在数据库中搜索和替换它。 也许有一个htaccess的解决方案?

旧: 黄狗总是点击www.domainname1.tld

新: 黄狗总是点击www.domainname2.tld

PHP中的

$bodytag = "the yellow dog clicks always www.domainname1.tld";

$bodytag = str_replace("domainname1.tld", "domainname2.tld", $bodytag);

3 个答案:

答案 0 :(得分:0)

更改文件内容超出了.htaccess文件或Apache的范围。

如果链接指向某个域,则会调用该域。您可以在该域上添加重定向(here's示例如何),但不能阻止请求在旧域中结束。

答案 1 :(得分:0)

我在htaccess中没有意识到这样做的方法。有一些Apache模块旨在操纵内容。例如,mod_substitute。

http://httpd.apache.org/docs/2.2/mod/mod_substitute.html

<Location />
    AddOutputFilterByType SUBSTITUTE text/html
    Substitute s/www.domainname1.tld/www.domainname2.tld/ni
</Location>

信用:http://corpocrat.com/2008/09/19/install-apache-mod_substitute/

答案 2 :(得分:0)

我有类似的情况,我们将大量文件移动到新的文件夹结构:

旧结构:

/css/
/images/
/js
/*.html

新结构:

/common/css/
/common/images/
/common/js
/<various_html_folders/*.html

问题是我的html文件通过相对路径引用了css,js和图像,并且使用新结构我将不得不更新数千个引用。

所以我遇到了Apache中的mod_rewrite,它在将响应主体发送到客户端之前重写了它。我将以下4行添加到我的.htaccess文件中,以便将所有href="css/更改为href="/common/css/"

AddOutputFilterByType SUBSTITUTE text/html
Substitute 's|href="css/|="/common/css/|i'
Substitute 's|href="js/|="/common/js/|i'
Substitute 's|href="images/|="/common/images/|i'

瞧! css,javascripts和images像以前一样工作而不更新我的任何html文件。

免责声明:我还没有研究这样做对性能的影响。请记住,对于您添加的每个Substitute行,都会对整个响应正文执行正则表达式。