在不同的环境中使用相同的.htaccess

时间:2010-01-25 05:56:19

标签: apache .htaccess mod-rewrite

从开发到测试再到生产,我们的网站位于三个不同的环境中

  • http:// localhost / version /
  • http://www.production.com/test/
  • http://www.production.com /

(请参阅以下.htaccess代码段中的网址示例)

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^brand/(.+) /products.php?brand=$1 [R,NC]
RewriteRule ^product/(.+) /products.php?model=$1 [R,NC]

如何捕获URL的第一部分并将其添加到.htaccess文件中,以便以下URL的行为相同:

  • http:// localhost / version / product / c20
  • http://www.production.com/test/product/c20
  • http://www.production.com/product/c20

我看过RewriteCond,但是在这种情况下我还没有发现如何使用它。请启发我。

2 个答案:

答案 0 :(得分:0)

编辑:这似乎有效:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.+)/(brand|product)/
RewriteRule ^brand/(.+) %1/x.php?brand=$1 [NC]
RewriteCond %{REQUEST_URI} ^(.+)/(brand|product)/
RewriteRule ^product/(.+) %1/x.php?model=$1 [NC]

答案 1 :(得分:0)

您可以将该部分设为可选:

RewriteRule ^(version/|test/)?brand/(.+) /$1products.php?brand=$2 [R,NC]
RewriteRule ^(version/|test/)?product/(.+) /$1products.php?model=$2 [R,NC]
相关问题