Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL rewriting in PHP without htaccess

Tags:

Website is running on a web host where we don't have access to a .htaccess file. However, I want to do URL rewriting for user friendly URLs.

e.g. Original URL

www.example.com/file?q=name 

expected URL

www.example.com/file/name 
like image 397
Vivart Avatar asked Jan 24 '10 11:01

Vivart


People also ask

Is URL rewriting safe?

The URL rewrite simply changes the presentation of variables in the url but does not secure at all.


1 Answers

As other people said, just use links like /index.php/nice/looking/url.
The "index.php" in the middle of the URL might look a little strange, but I don't think it's possible to have it look better without .htaccess

Else, you could ask your hoster to redirect any URL to /index.php so that you can handle URL rewriting without having /index.php in your URL.

Then you can just use a regex match to detect what file to include.
preg_match('@[/]{1}([a-zA-Z0-9]+)@', $_SERVER["PATH_INFO"], $matches) ($matches will contain all "parts" of the url in an array)

Be careful with including the files, use a whitelist so you're sure nobody would be able to load internal files.

like image 176
Daan Avatar answered Oct 25 '22 00:10

Daan