Rewrite the url to remove the question mark and add slashes to htaccess

I spent a couple of hours trying to achieve something that seemed easy to me. I http://localhost/testing_url_document/second.php?id=2also want to turn it http://localhost/testing_url_document/second/id/2. I achieved to remove the php extension, but was stuck in rewriting the site page. In htacces, I performed 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'];
?>

Second .php should get value 2

Thanks in advance for your help.

+4
source share
2 answers

Do it like this 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]
+1
source

This is the minimum configuration that can solve your problem:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^site=(.*)$ [NC]
RewriteRule index.php /index/site/%1? [R=301,L]
0
source

Source: https://habr.com/ru/post/1612887/


All Articles