2

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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

That kind of works... It solves the problem with /test/1/, but with just one variable, /test/, it throws the same error... Is there a way to split the rule so it matches either one or two variables , e.g. [/test/ OR, /test/1/] ?
Great! Thanks for the help!
It's been over a week, just continuing with some of the development (I've been busy...). how can I adapt this rule so that it also allows URLs without the GET variables? e.g. domain.com not domain.com/test/
I think you cannot hide all paths like /path/ or /test/ from your URLs. However I may have misunderstood the requirement. Better you post a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.