1

I have a subdomain like this:

Example: http://us.example.com/index.php?city=newyork

How do I rewrite to:

http://us.example.com/newyork

"us" is a virtual subdomain. So, It can be different: us, fr, it, etc..

I tried in this way but it does not work

RewriteEngine on

RewriteCond %{HTTP_HOST} !^example\.com
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*) index.php?city=$1
Anthony Lee
  • 223
  • 1
  • 6
  • 14

1 Answers1

1

Keep it like this:

RewriteEngine on

# external redirect from actual URL to pretty one
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php\?city=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?city=$1 [L,QSA]

EDIT:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php\?city=([^&]+)&ountry=([^\s&]*)\s [NC]
RewriteRule ^ /%1/%2? [R=302,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?city=$1&country=$2 [L,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Great!It's perfect! if I want to add another parameter for example: from http://us.example.com/index.php?city=newyork&country=usa to http://us.example.com/newyork/usa How can I do? – Anthony Lee Feb 14 '14 at 20:45
  • Thank you so much anubhava! but I have forgot to tell you that the second parameter must be not obligatory. I have no words to thank you enough! – Anthony Lee Feb 14 '14 at 20:57
  • like this work: http://us.example.com/newyork/usa instead like this does not work: http://us.example.com/newyork The second parameter in this case "usa" must be optional – Anthony Lee Feb 15 '14 at 18:50
  • It should work, did you combine both set of rules in your .htaccess? – anubhava Feb 16 '14 at 13:52
  • 1
    Sorry anubhava, I made a mistake :-) ..Everything is fine! Thank you sooo much!! – Anthony Lee Feb 16 '14 at 15:06