-2

I'm stuck trying to figure out the best approach to my problem. I want to pass query params with a / than a ?.

So for example, instead of:

https://example.com/search.php?q=test

It'd be:

https://example.com/search/test/

I'm not using any PHP framework. How can I reach my goal?

1
  • php.net/manual/fr/reserved.variables.server.php array_pop (explode ("/",$_SERVER['HTTP_HOST'])) will give you test when the url is https://example.com/search/test/. In your index explode the whole url and call the right php file in function (with a switch) Jul 17, 2020 at 13:47

2 Answers 2

3

You could do this with your .htaccess file.

For that you need to write in your .htaccess:

RewriteEngine on
RewriteRule ^search/([A-Za-z0-9\-]+)/?$ search.php?q=$1

The first line activate your RewriteEngine, so you can write RewriteRules as much you like.

In the second line you define your first RewriteRule:

In the URL it need to be search/([A-Za-z0-9\-]+)/. That means, you only allow the characters inside the square bracket. Then you redirect to your wanted script, in this case its search.php with the GET Parameter.

It's also possible to do this with more parameters or much more. For that you should google maybe "htaccess Explanation".

0
-1

Better try php router and dont waste the time for rewrite !!! And test nginx server it is awesome.

Not the answer you're looking for? Browse other questions tagged or ask your own question.