如何用htaccess做到这一点?

时间:2012-02-13 08:18:00

标签: .htaccess subdomain wildcard-subdomain

如何使用htaccess:

http://example.com/category => /index.php?action=category
http://example.com/category?query=string => /index.php?action=category&query=string
http://example.com/category/subcategory => /index.php?action=category/subcategory
http://subdomain.example.com => /index.php?action=subdomain
http://subdomain.example.com/category/subcategory => /index.php?action=subdomain/category/subcategory

这是我目前的代码:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)?$ /index.php?action=$1 [L]

2 个答案:

答案 0 :(得分:0)

以下重写规则将重写:

http://subdomain.domain.com => /index.php?action=subdomain/

我建议你在index.php中处理这个问题,以避免额外的重写规则。

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} ^(subdomain)\. [NC]

RewriteRule ^(.*)$ /index.php?action=%1/$1 [L,QSA]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} ^(domain)\. [NC]

RewriteRule ^(category)(^/.*)?$ /index.php?action=$1$2 [L,QSA]

答案 1 :(得分:0)

感谢您的帮助。

.htaccess代码:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)?$ /index.php?action=$1 [L]

index.php代码:

$domain = "domain.com";

if($_SERVER["HTTP_HOST"] != $domain)
{
    $subdomain = str_replace(".$domain","",$_SERVER["HTTP_HOST"]);
    $_GET['action'] = (isset($_GET['action'])) ? "$subdomain/".$_GET['action']:$subdomain;
}