2015-06-13 148 views
0

我想在URLs /文件夹末尾只有一个没有尾部斜线的URL,但同时我想重新编写此文件夹以便使用动态网页,例如:重写文件夹并删除末尾的斜杠

,如果我有:

mydomain.com/folder 
mydomain.com/folder/ 

mydomain.com/folder/second 
mydomain.com/folder/second/ 

那么我想只打开 “mydomain.com/folder” 和“mydomain.com/folder/第二个“,这个网址应该打开一个动态页面秒。这是我正在尝试,但它不工作,它崩溃我的页面:

RewriteEngine On 
RewriteRule ^(.*)/$ /$1 [L] 
RewriteRule ^(.*)$ includes/category.php?url=$1&pageType=category [L] 

RewriteRule ^(.*)/(.*)/$ /$1/$2 [L] 
RewriteRule ^(.*)/(.*)$ includes/subcategory.php?url=$1&pageType=subCategory [L] 

我在做什么错了?你能帮我运行正确的语法吗?

回答

1

您需要重定向末尾的斜杠URL而不是重写它们。您还可能需要一些条件添加到规则:

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/$ /$1 [L,R=301] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/]+)$ /includes/category.php?url=$1&pageType=category [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/]+)/([^/]+)$ /includes/category.php?url=$1&pageType=subCategory [L] 
+0

谢谢@JonLin它完美的工作! – Saymon

0
RewriteEngine On 
RewriteRule ^([^\/]+)/?$ includes/category.php?url=$1&pageType=category [L] 

RewriteRule ^([^\/]+)/([^\/]+)/?$ includes/subcategory.php?url=$1&pageType=subCategory [L] 

你还可以添加以下条件(重写之前),以排除那些规则真正的文件和文件夹:

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