多个重写规则,包含.htaccess中的变量

时间:2014-03-19 09:31:42

标签: php apache .htaccess mod-rewrite

我正在尝试使用htaccess重写规则。我需要

http://www.sitename.com/variable/upload.php 映射到
http://sitename.com/upload.php?slug=variable

http://www.sitename.com/variable/gallery.php 映射到
http://sitename.com/gallery.php?slug=variable

http://www.sitename.com/variable/
映射到 http://sitename.com/home.php?slug=variable

对于第三部分,我有:

Options -Multiviews

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([^/]+) home.php?slug=$1 [L]

但现在http://www.sitename.com/variable/upload.php也会映射到http://sitename.com/home.php?slug=variable

我该怎么做?

1 个答案:

答案 0 :(得分:1)

你的规则如下:

Options -Multiviews
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ $2?slug=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ home.php?slug=$1 [L,QSA]

即。在重写为home.php

之前处理2斜杠规则