各种各样的htaccess mod_rewrite

时间:2012-05-27 19:58:38

标签: php .htaccess mod-rewrite seo

我正在尝试为我的网站排序搜索引擎优化,但我似乎无法弄清楚这种情况。

我希望有4个查询参数,$cat$subcat$page$othervars

但我需要一个htaccess规则来允许这些值:

  1. domain.com/cat/page
  2. domain.com/cat/subcat/page
  3. domain.com/cat
  4. domain.com/cat/subcat/page/other
  5. 我怎样才能做到这一点,到目前为止,我所拥有的只是:

    RewriteEngine On
    RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /beta/index.php?cat=$1&cat2=$2&page=$3&var=$4 [L]
    

3 个答案:

答案 0 :(得分:1)

用PHP解析!

.htaccess:

RewriteEngine On
RewriteRule ^(.*)$ /beta/index.php?var=$1 [L]

...并在index.php中

$vars = explode("/",$_GET['var']);
$cat = isset($vars[0]) ? $vars[0] : false;
$subcat = isset($vars[1]) ? $vars[1] : false;
$page = isset($vars[2]) ? $vars[2] : false;
$othervars = isset($vars[3]) ? $vars[3] : false;

答案 1 :(得分:1)

RewriteEngine On
RewriteRule ^([^/]+)/?$ /beta/index.php?cat=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /beta/index.php?cat=$1&page=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /beta/index.php?cat=$1&cat2=$2&page=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /beta/index.php?cat=$1&cat2=$2&page=$3&var=$4 [L]

答案 2 :(得分:0)

我认为这些使用4 RewriteRules会更好:

RewriteEngine On
RewriteRule ^([^/]+)/?$ /beta/index.php?cat=$1 [L,R=301]
RewriteRule ^([^/]+)/([^/]+)/?$ /beta/index.php?cat=$1&page=$2 [L,R=301]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /beta/index.php?cat=$1&cat2=$2&page=$3 [L,R=301]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /beta/index.php?cat=$1&cat2=$2&page=$3&var=$4 [L,R=301]