简体   繁体   中英

Edit htaccess with php

Hi guys its possible and if yes how i can do this

on my htaccess on first line i have

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.

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.

I am guessing you're trying to rewrite URLs dynamically, if so, there is another approach you can take.

Forward every request to rewrite.php?req=request (through one simple rewrite in the .htaccess) RewriteRule ^(.*?)$ rewrite.php?req=$1

This would then eg rewrite home.php to 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. eg

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

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

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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