Rediect to $1 passing params and using pretty urls

after a lot Google/Youtube/Stackoverflow research and putting random things together I finally got something that partly works here:

ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
RewriteBase /SimpleMVCTest/


#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f

#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d 
#Rewrite the request to REQUESTED_FILENAME
RewriteRule ^(.*)$ $1.php?get=$1 [L]

There is two issues I have noticed with this

1: Giving www.example.com/car/1 will result in 500 Internal Error but if i write www.example.com/car it works fine. How do i make it so i can go car/1 and it will not give 500 internal Error

2: if i write www.example.com/somethingthatdoesnotexist or www.example.com/somethingthatdoesnotexist/1 it gives 500 internal but should just redirect to 404 but its not found

How would i do this?


Source: .htaccess

1 Comment

  1. Digin Dominic

    This is not a very good approach, you should not rewrite everything to a file like $1.php as you will be trying to request files like car/1.php which is not what you want: This will lead to the internal server error because on the next rewrite it will try to get car/1.php.php, then car/1.php.php.phpetc. etc.

    Instead you should rewrite everything to a fixed file like index.php and handle the logic in php. You can get the requested url (you could even add it as a query variable), parse it and include or redirect to any file you want.

    Reply

Leave a Reply