我今天开始学习重写
首先,这里是我的链接的例子
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!
有可能吗?
答案 0 :(得分:1)
以这种方式思考: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]