用php编辑htaccess

时间:2012-02-11 01:06:46

标签: php .htaccess

嗨,大家好吧,如果是的话我怎么做呢

在第一行的htaccess上我有

DirectoryIndex home.php index.php

然后

RewriteEngine On etc.....

基于我想要的代码选项

if (option=1) {
  // on htaccess write DirectoryIndex home.php index.php #first line
} else (option=2) {
  // on htaccess remove DirectoryIndex home.php index.php
}

我也不知道这是否安全

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

使用PHP的filesystem functions并像编辑其他任何文件一样进行编辑。

但是,它只会影响当前请求之后的请求,而不会影响进行更改的请求。如果这很重要,让PHP做与新.htaccess相同的事情。

答案 1 :(得分:0)

我猜你正在尝试动态地重写网址,如果是这样,你可以采取另一种方法。

将每个请求转发到rewrite.php?req = request(通过.htaccess中的一个简单重写) RewriteRule ^(.*?)$ rewrite.php?req=$1

这将是例如将home.php重写为rewrite.php?req = home.php

现在您可以访问所需的相关请求,在这种情况下home.php,您可以根据您的设置对此进行操作。 e.g。

<?php
$req = $_POST['req']; // need to sanitize this obv

if($option1) {
    // do something
} else if ($option2) {
    // something else
}
?>

希望有所帮助!