多国使用.htaccess

时间:2015-02-25 10:33:24

标签: apache .htaccess mod-rewrite

我正在努力实现以下任何一项:

  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
  3. 我浏览了一下,但遗憾的是还没有找到任何有效的解决方案。对于第一种,我尝试了以下代码:

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

    子域的问题在于它根本不会重定向到任何地方。我读了一遍,它说我需要设置通配符虚拟主机。我无法弄清楚如何在godaddy上做到这一点。所以不知道怎么做。

    至于第二种,我使用了以下代码:

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

    但这对我也不起作用。在这方面的任何帮助将受到高度赞赏。谢谢

    至于类别&子类别,子类别是强制性的。所以它也适用于以下链接:

    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
    3. 我不太确定如何判断.htaccess是否为$ 1 =子类别或$ 2 =子类别,因为他们在方案1& 2。

      感谢@anubhava的大力帮助,我已达到以下目的:

      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]
      

1 个答案:

答案 0 :(得分:1)

在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]