简体   繁体   English

mod_rewrite:带有两个 GET 变量的 URL 尾部斜杠

[英]mod_rewrite: Trailing slash on URL with two GET variables

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.我正在大修我的一个项目,一个基于网络的预订系统,但我的 htaccess 文件有点问题。 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.旧系统非常标准,网站路径中有 .php 脚本,我有一个隐藏扩展的规则,结果我有一个像 /viewinvoce?ID=1 这样的 URL。 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 以使其看起来更整洁 - 格式为 /viewinvoice/1,并且我正在到达那里,但我有一个小问题......

The URL /test works - it adds a trailing slash making the URL /test/, and the value 'test' is passed to the webpage. URL /test 有效 - 它添加一个尾部斜杠使 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/ 的工作原理如上,没有添加尾部斜杠,因为它已经存在,并且 'test' 被传递到网页。

The URL /test/1 also works, 'test' and '1' are both passed to the web page, URL /test/1 也有效,'test' 和 '1' 都被传递到网页,

but when a slash is type after 1 (/test/1/) the page throws a 404.但是当在 1 (/test/1/) 之后输入斜杠时,页面会抛出 404。

My .htaccess file我的 .htaccess 文件

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?PID=$1&ID=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])/(.*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

My simple PHP script..我的简单 PHP 脚本..

<?php
    echo $_GET['PID'];
    echo '<br>';
    echo $_GET['ID'];

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!理想情况下,我希望 .htaccess 文件为传递的第二个变量添加第二个斜杠,但此时我有点困惑,理想情况下,第二双眼睛会很有用!

Try these rules:试试这些规则:

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.请务必在清除浏览器缓存后对其进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM