How to control .htaccess from php or how to inject php code into .htaccess file

I'm not very good at .htaccess so I don't know how it works, I have a site and a link to my site: mysite.com/pagee.php?menu_id=1 I am using this .htaccess code:

<IfModule mod_rewrite.c>
RewriteEngine on

# 1-level
RewriteRule ^home/{0,1}$  pagee.php?menu_id=1 [QSA,L]


</IfModule>

      

and it works like this: mysite.com/home/

But my problem is that I have a lot of pages like:

mysite.com/pagee.php?menu_id=2, mysite.com/pagee.php?menu_id=3, mysite.com/pagee.php?menu_id=4, mysite.com/pagee.php?menu_id=5, mysite. com / pagee.php? menu_id = 6,

and my pagee.php code:

    <?php
require_once('config2.php');

$menu_id = intval($_GET['menu_id']);

$query = "SELECT * FROM menu WHERE `menu_id`=$menu_id";
$result = mysql_query($query);


while($row = mysql_fetch_assoc($result))
{

    echo '<h1>'.$row['mname'].'</h1>';

}

?>

      

pagee.php work with mysql

since while the code works internally .htaccess

like this:

    <IfModule mod_rewrite.c>
RewriteEngine on

# 1-level
<?php

$query = "SELECT * FROM menu WHERE `menu_id`=$menu_id";
$result = mysql_query($query);


while($row = mysql_fetch_assoc($result))
{
    $menu_id=$row['menu_id'];
    $linkname=$row['linkname'];


    echo 'RewriteRule ^'.$linkname.'/{0,1}$  pagee.php?menu_id='$menu_id' [QSA,L]';

} ?>


</IfModule>

      

+3


source to share


2 answers


PHP cannot control the .htaccess ok, but you can also control that by writing and rewriting the .htaccess file, I can figure it out, but I don't think it works correctly. But you can also try,

Made function

and inside that do this:

function edit_hta() {

    echo "<IfModule mod_rewrite.c> \r\n
\r\n RewriteEngine on \r\n";

    require('config2.php'); $getquery=mysql_query("SELECT * FROM menu ORDER BY menu_id DESC"); while($rows=mysql_fetch_assoc($getquery)){$menu_id=$rows['menu_id']; $linkname=$rows['linkname'];

echo "\r\n RewriteRule ^".$linkname."/{0,1}$  pagee.php?menu_id=".$menu_id. "[QSA,L] \r\n"; }

    echo "\r\n </IfModule>";

}

      

OK then create form, textarea button and submit

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
      <label>
        <textarea name="edit_ht" cols="45" rows="5"><?php echo edit_hta(); ?></textarea>
      </label>
      <input name="submit" type="submit" value="Edit" />
    </form>

      



Do php coding inside this page:

$submit = $_POST['submit'];

if ($submit) {

$edit_ht = file_put_contents('.htaccess', $_POST['edit_ht']);

}

      

I hope this can do well, so the main thing is, if you have many pages inside your sql database, you first need to create whatever you can see inside your sheet, all your links displayed inside the textbox like:

<IfModule mod_rewrite.c> 


 RewriteEngine on 

RewriteRule ^asasasa/{0,1}$  pagee.php?menu_id=10[QSA,L]

RewriteRule ^zxzxzx/{0,1}$  pagee.php?menu_id=9[QSA,L]

RewriteRule ^movie/{0,1}$  pagee.php?menu_id=8[QSA,L] 

 RewriteRule ^song/{0,1}$  pagee.php?menu_id=7[QSA,L] 

 RewriteRule ^software/{0,1}$  pagee.php?menu_id=6[QSA,L] 

 RewriteRule ^home/{0,1}$  pagee.php?menu_id=5[QSA,L]

 </IfModule>

      

then you click the "Edit" button and your .htaccess file will be edited and with this method you can manage your .htaccess file. Hope you can understand if you do not comment. Thank.

+1


source


Try the following:

RewriteEngine on

RewriteRule ^home/?$ /page.php?menu_id=$1 [QSA,NC,L]

RewriteRule ^music/?$ /page.php?menu_id=$2 [QSA,NC,L]

RewriteRule ^movie/?$ /page.php?menu_id=$3 [QSA,NC,L] 

RewriteRule ^link_name4/?$ /page.php?menu_id=$4 [QSA,NC,L]

RewriteRule ^link_name5/?$ /page.php?menu_id=$5 [QSA,NC,L] 


RewriteRule ^link_name6/?$ /page.php?menu_id=$6 [QSA,NC,L] 

      



Make sure to rename pathname 4,5,6 to your pathname

+1


source







All Articles