1

如何使用 htacess 制作:

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

2 回答 2

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]
于 2012-02-13T09:03:09.817 回答
0

感谢您的帮助。

.ht 访问代码:

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;
}
于 2012-02-13T18:27:09.007 回答