Jump to content

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 OnRewriteRule ^([^/]*)/([^/]*)$ /index.php?PID=$1&ID=$2 [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.*[^/])$ http://%{HTTP_HOST}/$1$2 [L,R=301]

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

R.E.V.O.


Realise       Every       Victory      Outright

Link to comment
https://linustechtips.com/topic/471514-mod_rewrite-and-get-requests/
Share on other sites

Link to post
Share on other sites

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 OnRewriteRule ^([^/]*)/([^/]*)$ /index.php?PID=$1&ID=$2 [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.*[^/])$ http://%{HTTP_HOST}/$1$2 [L,R=301]

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

 

I can't really help you with the RewriteEngine, I never really used it and never really got into it. What i did instead and what I would recommend you, is to write your own router in PHP or use a Framework (Slim PHP framework for example). I mean, to redirect all URLs to one file and handle the requests there. 

 

I don't know if it's the best solution, but it gives you a lot more control. 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×