使用PHP,我怎么可能将文件夹下的所有页面redirect到不同的域?
当前网站:
http://www.example.org/dept http://www.example.org/dept/stuff http://www.example.org/dept/more http://www.example.org/dept/more/stuff
新网站:
http://www.example-too.org/pets/stuff http://www.example-too.org/pets/more http://www.example-too.org/pets/more/stuff
我了解了如何redirect单个页面:
<? Header( "HTTP/1.1 301 Moved Permanently" ); Header( "Location: http://www.example-too.org/pets/more/stuff" ); ?>
但如何将这个应用到数十页,而不是每个创build一个PHPredirect?
我知道使用web服务器configuration(apache mod_rewrite)和/或.htaccess是处理多重redirect这样的最好方法,但我这些选项不提供给我。
谢谢。
不幸的是,除非已经使用一个PHP脚本来处理所有这些URL,否则您需要为每个URL创build一个新的PHP脚本。
你可以写一些脚本来自动完成这个过程。
例如,如果它是一个Linux服务器,并且你可以运行shell脚本,像这样的东西可以工作(在htdocs文件夹中执行,或等于):
#!/bin/bash # Add folders here FOLDERS=stuff more more/stuff for folder in $FOLDERS; do { echo '<?php' echo 'header("HTTP/1.1 301 Moved Permanently");' echo "header(\"Location: http://www.example-too.org/pets/$folder\");" } > dept/$folder/index.php done
如果您无法运行shell脚本,则可以将该脚本翻译成PHP。
值得一提的是,通过Web服务器configuration或.htaccess中的“redirect”命令来实现这一点的更好方法
感谢Mikael,我修改了你的脚本来处理我的html页面而不是文件夹:
#!/bin/env bash DEST=http://www.example-too.org/pets FILES=`find . |grep \.html$ -` for xfile in $FILES; do file=${xfile:2} # strip leading ./ file=${file%%.html}.php # change extension from .html to .php # comment out preceeding line to overwrite source .html { echo '<?php' echo 'header("HTTP/1.1 301 Moved Permanently");' echo "header(\"Location: $DEST/$file\");" echo '?>' } > $file echo created $file done