Alvaro Louzada
Alvaro Louzada

Reputation: 433

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

Upvotes: 0

Views: 1108

Answers (2)

GManz
GManz

Reputation: 1659

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 e.g. 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. e.g.

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

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

Hope that helps!

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

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.

Upvotes: 1

Related Questions