mod_rewrite: Trailing slash on URL with two GET variables

坚强是说给别人听的谎言 提交于 2021-01-28 00:04:41

问题


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...

The URL /test works - it adds a trailing slash making the URL /test/, and the value 'test' is passed to the webpage.

The URL /test/ works as above, a trailing slash isn't added since it already exists, and 'test' is passed to the webpage.

The URL /test/1 also works, 'test' and '1' are both passed to the web page,

but when a slash is type after 1 (/test/1/) the page throws a 404.

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]

My simple PHP script..

<?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!


回答1:


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.



来源:https://stackoverflow.com/questions/33299208/mod-rewrite-trailing-slash-on-url-with-two-get-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!