2017-02-16 121 views
3

我正在尝试创建一个htaccess脚本来为零,一个或两个变量创建目录。如何通过.htaccess制作动态目录和子目录

我正在使用的文件接受以下2个获取变量。制造和模型。

这是一个3步骤页面。我目前的页面位于/new.php。这个页面允许用户选择第一个变量(在我的情况下是车辆制造)。通过在此页面上选择一个品牌,用户将被带到/new.php?make=Acura。此页面现在显示所有Acura型号的列表。从这里,用户可以点击一个模型,他们将被定向到/new.php?make=Acura & model = TLX。他们现在可以选择一个子模型并将被带到一个信息页面。

所以我试图让:

new.php to go to /new/ 
new.php?make=XMake to go to /new/XMake/ 
and new.php?make=XMake&model=XModel to go to /new/XMake/XModel/ 

这是据我已经得到了我的代码:

RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteRule ^new/(.*)$ new.php?make=$1 [L,NC] 

不过,我在此之后添加任何变量似乎打破第一目录?为什么是这样?

回答

1

您可以在网站根目录的.htaccess使用这些规则:

Options -MultiViews 
RewriteEngine On 

# skip all files and directories from rewrite rules below 
RewriteCond %{REQUEST_FILENAME} -d [OR] 
RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule^- [L] 

RewriteRule ^new/([\w-]+)/([\w-]+)/?$ new.php?make=$1&model=$2 [L,NC,QSA] 

RewriteRule ^new/([\w-]+)/?$ new.php?make=$1 [L,NC,QSA] 

RewriteRule ^new/?$ new.php [L,NC] 
1

规则的顺序很重要。有了这条规则,任何请求/new/,/new/XMake/new/XMake/XModel/匹配,并且以下规则将被忽略。

为了符合其他规则,更具体的必须先来,例如,

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^new/(.+?)/(.+)$ new.php?make=$1&model=$2 [L,NC] 

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

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^new/$ new.php [L,NC]