-1

I'm a beginner in PHP, there is a one thing which I haven't been able to find an answer or I just don't know what to search.

I've made a PHP script which shows user info from database using GET parameter.

The url looks like this http://127.0.0.1/user-info.php?userid=testuser what I want to do is to make the url look like this http://127.0.0.1/user-info/testuser. How is this possible, is it done with .htaccess or using php and how?

Thank you in advance.

  • 1
    Possible duplicate of [How to create friendly URL in php?](https://stackoverflow.com/questions/812571/how-to-create-friendly-url-in-php) – lovelace Jan 12 '19 at 20:12
  • Possible duplicate of [How to create friendly URL in php?](https://stackoverflow.com/questions/812571/how-to-create-friendly-url-in-php) – d3jn Jan 12 '19 at 22:50

3 Answers3

1

Sure this is possible, rewriting is usually done on the protocol level, so inside the http server, it has nothing to do with the php engine:

RewriteEngine on
RewriteRule ^/?user-info/(.+)$ /user-info.php?userid=$1 [END]

This rule works likewise in the http server's host configuration or in a dynamic configuration file (".htaccess" style file). For this to work the http server's rewriting module has to be enabled, obviously. And if you decide to use a dynamic configuration script that also will have to be supported and enabled.

In case you receive an "internal server error" (http status 500) using above rule then chances are that you operate a very old version of the apache http server. In that case replace the END flag with the L flag, should work too in this case, though it depends on other rewriting rules you have. In any case you will find a definite hint on the unknown END flag in the http servers error log file.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

arkascha
  • 41,620
  • 7
  • 58
  • 90
0

I think you need something like this, give it a try. Just replace url(index.php with user-info.php)

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]

OR

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
-1

You can get a .htaccess file from zend expressive, it has everything you need - https://github.com/zendframework/zend-expressive-skeleton/blob/master/public/.htaccess. It passes all requests to index.php file where you can do further routing and then request processing.

As a router I recommend FastRoute (https://github.com/nikic/FastRoute). Also take a look at composer - it is a must have tool for php developer.