Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using spaces in URL and .htaccess

I wrote a local name/phone/address search engine for my city.

Users must be able to quick-access the results by going to either of these urls:

  1. search by number
    • http://domain.com/5554651
  2. search by lastname
    • http://domain.com/smith
    • http://domain.com/smith%20johnson
  3. search by lastname and first name
    • http://domain.com/smith/andrew
    • http://domain.com/smith%20johnson/mary%20elizabeth

This is my current .htaccess config:

# Smart links
RewriteRule ^([0-9]+)$ /html/index.php?phone=$1 [QSA,L]
RewriteRule ^([A-Za-z-]+)$ /html/index.php?lastname=$1 [QSA,L]
RewriteRule ^([A-Za-z-]+)/([A-Za-z-]+)$ /html/index.php?lastname=$1&name=$2 [QSA,L]

That works pretty well, except if the user includes a space in the lastname and/or first name. Also, no numbers can be used when searching for names.

Any ideas on how to allow spaces in the url? Thanks!

like image 821
Andres SK Avatar asked Feb 01 '11 20:02

Andres SK


2 Answers

Can you try this? ([a-z-\s]+)

RewriteRule ^([0-9]+)$ /html/index.php?phone=$1 [QSA,L]
RewriteRule ^([A-Za-z-\s]+)$ /html/index.php?lastname=$1 [QSA,L]
RewriteRule ^([A-Za-z-\s]+)/([A-Za-z-\s]+)$ /html/index.php?lastname=$1&name=$2 [QSA,L]
like image 200
borayeris Avatar answered Sep 23 '22 04:09

borayeris


RewriteRule ^([A-Za-z-\s]+)/([A-Za-z-\s]+)$ /index.php?lastname=$1&name=$2 [QSA,L]

The \s will take care of blank spaces.

like image 20
Andres SK Avatar answered Sep 24 '22 04:09

Andres SK