3

I'm not so good in .htaccess, so I don't know how to it work, I have a site and my site link like that: mysite.com/pagee.php?menu_id=1 I use 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 work like that: mysite.com/home/

But my problem is I have lot of page 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 is:

    <?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

so how to while code work inside the .htaccess like that:

    <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>
Asif Uz Zaman
  • 337
  • 1
  • 14
  • 1
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 29 '15 at 14:54
  • Try a solution where all calls are redirected to pagee.php. Then validate the request to your database table. If valid, continue, if not found, 404 header – sanderbee May 29 '15 at 15:01
  • 1
    Do you want a link like domain/menu/1 ? – Amit Verma May 29 '15 at 15:12
  • @Starkeen: No I wont to make like that domain.com/home. – Asif Uz Zaman May 29 '15 at 15:17
  • @Starkeen: Sir inside my mysql db I have lose of link to generated, but my .htaccess file do not have auto generate like my pagee.php? – Asif Uz Zaman May 29 '15 at 15:23
  • @Starkeen: Sir I wont to generate only this line of .htaccess code RewriteRule ^music/{0,1}$ pagee.php?menu_id=2 [QSA,L] RewriteRule ^movie/{0,1}$ pagee.php?menu_id=3 [QSA,L] – Asif Uz Zaman May 29 '15 at 15:25
  • @Starkeen: I can not do that with manually – Asif Uz Zaman May 29 '15 at 15:27

2 Answers2

2

PHP can not control .htaccess ok, but you also can control that by Write and Rewrite the .htaccess file, I can figure that but I don't think this properly work. But you can try that also,

Made a 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 make form, textarea and submit button

<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>

Than make php coding inside that page:

$submit = $_POST['submit'];

if ($submit) {

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

}

I hope that can do well, so the major thing is if you have a lot of pages inside you sql database first you have to create that all than you can see inside your from sheet, all of you links showing inside the textarea like this:

<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 hit Edit button then your .htaccess file will edit, and with this method you can control your .htaccess file. I hope you understand if you don't please give me comment. Thank you.

Softbazz
  • 202
  • 1
  • 9
1

Try this :

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] 

Be sure to rename the Path name 4,5,6 to your path name

Amit Verma
  • 40,709
  • 21
  • 93
  • 115