Saad Bashir
Saad Bashir

Reputation: 4519

Multi-Country Using .htacces

I am trying to achieve any one of the following:

  1. http://ca.domain.com/Category/SubCategory -> http://www.domain.com/index.php?country=ca&category=Category&subcategory=Subcategory
  2. http://www.domain.com/ca/Category/SubCategory -> http://www.domain.com/index.php?country=ca&category=Category&subcategory=Subcategory

I have browsed around but unfortunately haven't been able to figure out any working solution. For the first kind I tried the following code:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^(.*)/$ http://www.domain.com/index.php&country=%1&category=$1 [L]

The issue with sub domain is that it doesn't redirect anywhere at all. I read around and it says I need to setup wildcard virtual host. I haven't been able to figure out how to do that on godaddy. So not sure how to do it.

As for the second kind, I used the following code:

RewriteEngine on 

RewriteRule ^(.*)/?(.*)/ home.php?country=$1&category=$2 [B]

But that didn't work for me either. Any help in this regard would be highly appreciated. Thank you

As for Category & Subcategory, Subcategory is kind of mandatory. So it should also work for following link:

  1. http://www.domain.com/ca/Toronto/Programming -> http://www.domain.com/index.php?country=ca&location=Toronto&subcategory=Programming
  2. http://www.domain.com/ca/Programming -> http://www.domain.com/index.php?country=ca&location=&subcategory=Programming

I am not quite sure as to how to tell .htaccess if it is $1=subcategory or $2=subcategory since they switch in scenario 1 & 2.

Thanks to immense help from @anubhava I have reached to the following:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/?$ adlisting.php?country=%1&category=$1 [L,QSA]

RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ adlisting.php?country=%1&location=$1&category=$2 [L,QSA]

RewriteCond %{HTTP_HOST} ^(www\.)?\.domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/?$ adlisting.php?country=$1&category=$2 [L,QSA,NC]

RewriteCond %{HTTP_HOST} ^(www\.)?\.domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/([^/]+)/?$ adlisting.php?country=$1&category=$2&subcategory=$3 [L,QSA,NC]

Upvotes: 1

Views: 73

Answers (1)

anubhava
anubhava

Reputation: 784968

Have these rules in root .htaccess:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/?$ index.php?country=%1&category=$1 [L,QSA]

RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?country=%1&category=$1&subcategory=$2 [L,QSA]

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/?$ index.php?country=$1&category=$2 [L,QSA,NC]

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^([a-z]{2})/([^/]+)/([^/]+)/?$ index.php?country=$1&category=$2&subcategory=$3 [L,QSA,NC]

Upvotes: 1

Related Questions