显示没有父文件夹的特定(动态)URL。让其他人独自

时间:2016-05-01 12:36:31

标签: .htaccess url hyperlink uri

我今天开始学习重写

首先,这里是我的链接的例子

Index.php?action=pictures
Index.php?action=pictures&type=kitchen
Index.php?action=pictures&type=ceiling
Index.php?action=services
Index.php?action=services&type=shower
Index.php?action=services&type=windows

在我的.htaccess文件中,我有这个

RewriteEngine On
RewriteBase /
#Do not Rewrite files or folders
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^(.*?)$ $1 [L]

#Ordinary
RewriteRule ^\.htaccess$ .htaccess [F]
RewriteRule ^(.*)/(.*)/?$ Index.php?action=$1&type=$2 [L]
RewriteRule ^(.*)/?$ Index.php?action=$1 [L]

将网址重写为

localhost/pictures
localhost/pictures/kitchen
localhost/pictures/ceiling

localhost/services
localhost/services/shower
localhost/services/windows

//etc

我希望我的所有链接都按原样运行,除了我不需要父文件夹/services/的服务。 结果:

localhost/pictures
localhost/pictures/kitchen
localhost/pictures/ceiling

localhost/services
localhost/shower
localhost/window

我试图重写.htaccess,但要么只让父文件夹工作,要么只有子文件夹。 我试图添加这个,但我知道这匹配所有内容......

RewriteRule ^(.*)/?$ Index.php?action=services&type=$1 [L]
然而,我可以像这样硬编码

RewriteRule ^window/?$ Index.php?action=services&type=window [L]

想要有动力的东西。如果文件夹服务 - >显示没有文件夹但仍然可以看到localhost/services! 有可能吗?

1 个答案:

答案 0 :(得分:1)

以这种方式思考:Apache如何知道特定字符串是一个动作还是一种服务?

嗯,你有三个选择:

  • 我们对服务类型进行硬编码。任何与类型不匹配的东西都必须是一个动作。
  • 我们对行动进行硬编码。因此,任何与行动不匹配的行为都必须是一种服务。
  • Apache无法知道:我们将它提供给一个脚本,该脚本可能会做一些魔术来找出这个字符串是什么。

在您的情况下,硬编码操作似乎是最好的主意,至少在操作是静态的时候。

RewriteRule ^(.*)/(.*)/?$ Index.php?action=$1&type=$2 [L]
RewriteRule ^(pictures|services)/?$ Index.php?action=$1 [L]
RewriteRule ^(.*)/?$ Index.php?action=services&type=$1 [L]
相关问题