mod_rewrite:带有两个 GET 变量的 URL 尾部斜杠
我正在大修我的一个项目,一个基于网络的预订系统,但我的 htaccess 文件有点问题.旧系统非常标准,网站路径中有 .php 脚本,我有一个隐藏扩展的规则,结果我有一个像/viewinvoce?ID=1 这样的 URL.我一直在尝试找出如何重写此 URL 以使其看起来更整洁 - 格式为/viewinvoice/1,并且我正在到达那里,但我有一个小问题...
I'm in the process of overhauling one of my projects, a web based booking system, but I'm having a bit of an issue with my htaccess file. The old system was pretty standard, with .php scripts in the route of the website, I had a rule hiding my extensions, and I resultantly had a URL like /viewinvoce?ID=1. I've been trying to find out how to rewrite this URL so it looks a lot neater - in the format /viewinvoice/1, and I'm getting there, but I have a slight problem...
URL/test 有效 - 它添加了一个尾部斜杠使 URL/test/,并且值 'test' 被传递到网页.
The URL /test works - it adds a trailing slash making the URL /test/, and the value 'test' is passed to the webpage.
URL/test/的工作原理如上,没有添加尾部斜杠,因为它已经存在,并且 'test' 被传递到网页.
The URL /test/ works as above, a trailing slash isn't added since it already exists, and 'test' is passed to the webpage.
URL/test/1 也有效,'test' 和 '1' 都传递给网页,
The URL /test/1 also works, 'test' and '1' are both passed to the web page,
但是当在 1 (/test/1/) 之后输入斜杠时,页面会抛出 404.
but when a slash is type after 1 (/test/1/) the page throws a 404.
我的 .htaccess 文件
My .htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?PID=$1&ID=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])/(.*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
我的简单 PHP 脚本..
My simple PHP script..
<?php
echo $_GET['PID'];
echo '<br>';
echo $_GET['ID'];
理想情况下,我希望 .htaccess 文件为传递的第二个变量添加第二个斜杠,但此时我有点困惑,理想情况下,第二双眼睛会很有用!
Ideally, I'd like the .htaccess file to add a second trailing slash to the second variable passed, but I'm a bit confused at this point, and ideally a second pair of eyes would be useful!
试试这些规则:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !/$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?PID=$1&ID=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?PID=$1 [L,QSA]
请务必在清除浏览器缓存后对其进行测试.
Make sure to test it after clearing your browser cache.