简体   繁体   English

用php编辑htaccess

[英]Edit htaccess with php

Hi guys its possible and if yes how i can do this 大家好,可能的话,我该怎么做

on my htaccess on first line i have 在第一行的htaccess中,我有

DirectoryIndex home.php index.php

then 然后

RewriteEngine On etc.....

so based on a option on my code i want to do 所以根据我想做的一个选项

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

also i dont know if this is safe 我也不知道这是否安全

thanks for any help 谢谢你的帮助

Use PHP's filesystem functions and edit it just like any other file. 使用PHP的文件系统功能并像其他任何文件一样对其进行编辑。

However, it will only affect requests after the current one, not the one that makes the change. 但是,它将仅影响当前请求之后的请求,而不影响进行更改的请求。 If this matters, have PHP do the same thing as the new .htaccess would. 如果这很重要,请让PHP做与新的.htaccess相同的事情。

I am guessing you're trying to rewrite URLs dynamically, if so, there is another approach you can take. 我猜您正在尝试动态重写URL,如果是这样,则可以采用另一种方法。

Forward every request to rewrite.php?req=request (through one simple rewrite in the .htaccess) RewriteRule ^(.*?)$ rewrite.php?req=$1 转发每个rewrite.php?req = request请求(通过.htaccess中的一个简单重写) RewriteRule ^(.*?)$ rewrite.php?req=$1

This would then eg rewrite home.php to rewrite.php?req=home.php 然后,例如,将home.php重写为rewrite.php?req = home.php

Now you can access the relevant request you want, in this case home.php , you could then act on this based on your settings. 现在,您可以访问所需的相关请求,在本例中为home.php ,然后可以根据您的设置对此进行操作。 eg 例如

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

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

Hope that helps! 希望有帮助!

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

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