Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite url to remove question mark and add slashes in htaccess

I've spent a couple of hours trying to achieve something I thought was easy. I have http://localhost/testing_url_document/second.php?id=2 and want to turn it http://localhost/testing_url_document/second/id/2. I have achieved to remove the php extension but stucked into the rewriting the site page . In the htacces i have followed the following procedure.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

this is my index page

Index.php

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        ?>
        <a href="second/id/2">click here</a>
    </body>
</html>

Second.php

<?php 
 echo $_GET['id'];
?>

The second.php should gets the value 2

Thanks in advance for help.

like image 822
Harshit Sethi Avatar asked Oct 18 '22 23:10

Harshit Sethi


1 Answers

Have it this way inside /testing_url_document/.htaccess:

RewriteEngine On
RewriteBase /testing_url_document/

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^./]+)(/.*)?$ $1.php$2 [L]

RewriteRule ^([\w-]+(?:\.php)?)/([\w-]+)/([\w-]+)/?$ $1?$2=$3 [L,QSA,NC]
like image 65
anubhava Avatar answered Oct 24 '22 13:10

anubhava