.htaccess 用页面名称斜杠 GET 参数重写 url

问题描述

我正在尝试将我的 URL 从 request/2 重写为 request?number=2

我试图在我的.htaccess

RewriteEngine ON

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^request/(.*)$ request?number=$1 [NC,L]

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

但结果是 404 Error

有人可以帮我吗?

解决方法

根据您展示的样品,您可以尝试以下操作吗?在测试您的网址之前,请务必清除浏览器缓存。

RewriteEngine ON
##To remove .php extension eg--> someone hits http://localhost:80/test.php it will redirect to http://localhost:80/test  
RewriteCond %{THE_REQUEST} \s([^.]*)\.php\s [NC]
RewriteRule ^ %1 [R=301]
 #RewriteRule ^(.*)$ $1.php [L]

##All non-existing files/directories will come in this rule whose URI starts from request. 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^request/([\w-]+)/?$ request.php?number=$1 [NC,L,QSA]

##All non-existing files/directories will come in this rule set apart from URI which starts from request which is taken care above.   
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]