url重写不起作用.htaccess


url rewrite not working .htaccess

我正在尝试使用htaccess重写url,我已经尝试了这里给出的另一个问题的答案,但似乎什么都不起作用,我仍然得到了原始url。这就是我的

http://localhost/inbox.php?pg=2

我想要

http://localhost/inbox/2 

我已经有了一个规则,如下所示,去掉了.htaccess中的.php扩展,并添加了最后一行

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}'.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
RewriteRule /(.*)$ /inbox.php?pg=$1//added this

您的问题是最后一行之前的行被定义为最后一行,因此您的规则必须高于RewriteConditions。更好地使用这个规则集:

RewriteRule ^/?inbox/('d+)$ /inbox.php?pg=$1 [QSD,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}'.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]

我添加了你需要的前缀,但你错过了,并规定在那之后会有数字(至少一个)。

Apache重写引擎主要用于将动态url(如http://localhost/inbox.php?pg=2)转换为静态和用户友好的url(http://localhost/inbox/2

RewriteEngine on
RewriteRule ^inbox/([^/.]+)/?$ /inbox.php?pg=$1 [L]

说明:这是如何工作的

行动要求:重写规则

模式:^收件箱/([^/.]+)/?$

重写:/inbox.php?pg=$1

命令标志:[L]

简单地说,

RewriteEngine on
RewriteRule ^inbox/([0-9]+)/?$ inbox.php?pg=$1

来源:使用htaccess 重写的10个简单示例