0

I'm using a single index.php using switch...

My .htaccess is currently:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ index.php?page=$1 [NC,L]

Usually I would settle for this option but it will just end up in conflicts down the track with other stuff how can I replicate this in php taking it off Apache.

By the file format above the https://example.com/index.php?page=about turns each case into https://example.com/about.

I can't find a simple guidance and how to achieve this in PHP and I know it is possible.

Joe
  • 4,877
  • 5
  • 30
  • 51
Joshua
  • 1
  • 1

1 Answers1

0

It is defenitly not possible. At some point you have to tell your webserver what to do with the request, it cannot guess by itself that you are running a php script.

A best practise for web applications is to map every request to your index.php (unless the file or directory exists) and then you can evaluate and route the request within your application on your behalf, e.g. something like this (from laravel):

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

You must not necessarily do this in you .htaccess, you could also put this in you vhost config or theoretically also in your httpd.conf.

I can't see how this could end up in any conflicts, maybe you can explain your concerns regarding this a little bit more.

passioncoder
  • 909
  • 6
  • 10
  • Not best, just common. It's half the reason WordPress has so many exploits and they're hard to clean up because they add more backdoors. – Walf Feb 03 '17 at 10:30
  • you're probably right, since I don't work with wordpress I'm not aware if there are any pitfalls with this solution. updated my answer. – passioncoder Feb 03 '17 at 14:20