重写url以删除问号并在htaccess中添加斜杠


Rewrite url to remove question mark and add slashes in htaccess

我花了几个小时试图实现一些我认为很容易的事情。我有http://localhost/testing_url_document/second.php?id=2,想把它变成http://localhost/testing_url_document/second/id/2。我已经实现了删除php扩展,但陷入重写网站页面。在访问中,我遵循了以下步骤。

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

这是我的索引页

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应该得到值2

提前感谢您的帮助。

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

这是可以解决您的问题的最小配置:

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