Htaccess redirect using mod_rewrite for friendly urls

I am trying to trim my urls using htaccess when my index.php acts like a key with the following code:

RewriteEngine on RewriteRule ^([^/]+)$ index.php?city=$1 [NC] 

To use this in my area, just add the following to work on mysite.com/members/index.php?city=$1

  RewriteRule ^members/([^/]+)$ index.php?city=$1 [NC] 

in the case of a city that has a few words, that is: the city of New York, what is the best way to approach this? defining it in .htaccess (not sure if possible) to change the city of New York โ†’ New York City or virtually any dash space. Or, do it in php with a function to replace '' โ†’ '-' for db requests that use $ _GET?

+4
source share
1 answer

I would replace the spaces with strokes in PHP and then redirect to a new version of the URL to get easy to read addresses. (The space is encoded as% 20, which is not quite easy to read)

Thus, you can still enter the address domain.com/new york city , but will be redirected to domain.com/new-york-city instead of domain.com/new%20york%20city .

Replacing in .htaccess is possible with something like this:

 RewriteRule ^([^\s]*)\s(.*) $1-$2 [N] 

(Based on the response to Search and replace in apache htaccess RewriteRule )

+5
source

Source: https://habr.com/ru/post/1382364/


All Articles