How to make restful like urls

40 Views Asked by At

Presently I got a url that is passed as such domain.tld/pages.php/parameter then the pages.php script simply looks at the $_SERVER["REQUEST_URL"] to make the necessary changes to the page.

I have looked up a number of explanations about how to remove the .php from the url, however all of them break the /parameter portion of the url and the page simply won't load. If I look up the closest facsimile it is "restful" urls, but they pass the parameters to another script like index.php or api.php which is not my goal. I have otherpage.php that should work all the same.

Appreciate any help.

1

There are 1 best solutions below

1
Sultan Shindi On

You just have to use rewrite rules on .htaccess to change the urls' direction and rules (in case of Apache webserver).

  1. Firstly create a .htaccess file on the base website directory where your index.php or whatever files lies.
  2. Then, write the below code on the created .htaccess file:
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
  1. Visit any file of your website without writing .php extension, for example: if you have a file called otherpage.php and the previous working url path to it was https://domain.tld/otherpage.php then now you could visit the same page with this url https://domain.tld/otherpage.