2012-02-13 65 views
1

如何使用htacess使这个:如何使用htacess进行此操作?

http://domain.com/category => /index.php?action=category 
http://domain.com/category?query=string => /index.php?action=category&query=string 
http://domain.com/category/subcategory => /index.php?action=category/subcategory 
http://subdomain.domain.com => /index.php?action=subdomain 
http://subdomain.domain.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] 
+0

你做了什么/试过这么远? – MrJ 2012-02-13 08:20:56

+0

我刚做了前三行 – Aghaie 2012-02-13 08:43:21

+0

它工作吗? .htaccess代码在哪里?因为我没有看到它! – MrJ 2012-02-13 08:45:02

回答

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; 
} 
+0

通过编辑将任何代码发布到您的问题本身。通过评论答案也可以通知答题者。这个代码的目的是什么?在评论中使用@username(如@Aghale)来通知用户。 – ThinkingMonkey 2012-02-14 01:47:44

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]