如何写一个'root'RewriteRule重定向到两个不同的页面?

时间:2013-01-03 11:36:59

标签: php apache .htaccess mod-rewrite url-rewriting

我正在努力.htaccess,我有这个代码:

<pre>
# PRODUCTS
RewriteRule ^(.*)/(.*)$     /newwebsite/product.php?category=$1&product=$2 [L,NC]

# SERVICES
RewriteRule ^(.*)/(.*)$     /newwebsite/service.php?category=$1&service=$2 [L,NC]

# PLAIN PAGES
RewriteRule ^/$                             /newwebsite/index.php  [L,NC]
RewriteRule ^signup-free$                   /newwebsite/signup-free.php  [L,NC]
RewriteRule ^about$                         /newwebsite/content.php?page=1 [L,NC]
RewriteRule ^portfolio$                     /newwebsite/portfolio.php [L,NC]
RewriteRule ^our-stores$                    /newwebsite/our-stores.php  [L,NC]
RewriteRule ^contact$                       /newwebsite/contact.php  [L,NC]
RewriteRule ^products-list$                 /newwebsite/lists.php?action=products  [L,NC]
RewriteRule ^services-list$                 /newwebsite/lists.php?action=services  [L,NC]
</pre>

我的代码工作不正常,我需要在产品和服务规则中添加一些东西,使它们分别重定向到'product.php'和'service.php',而不再添加'斜杠'或'子文件夹',但我不知道是什么以及如何,我的意思是我只需要:

http://www.root.com/maintenance-services/weekly-pool-service  
  redirects to =>  
http://www.root.com/service.php?category=maintenance-services&service=weekly-pool-service

但与此同时我需要:

http://www.root.com/hot-tubs/jacuzzi 
  redirects to =>  
http://www.root.com/product.php?category=hot-tubs&product=jacuzzi

如何在产品和服务之间做出改变,如何告诉apache使用相同的参数转到不同的页面?

我很困惑。非常感谢你。

1 个答案:

答案 0 :(得分:2)

制作档案rewrite.php,将所有内容重定向:

RewriteRule ^(.*)/(.*)$     /newwebsite/rewrite.php?sub=$1&second=$2 [L,NC]

将所有逻辑放在此文件中。

伪代码

if($_GET['sub'] exists in table "service") {
   $_GET['category'] = $_GET['sub'];
   $_GET['service'] = $_GET['second'];
   include 'service.php';
}else{
   ...
}