简体   繁体   English

重写文件夹并删除末尾的斜杠

[英]Rewrite folders and Remove the trailing slash at the end

I'm trying to have only one URL without the trailing slash at the end of the URLs/folders, but at the same time I want to re-write this folder to use dynamic pages, for example: 我正在尝试仅使用一个URL,但在URL /文件夹末尾不加反斜杠,但同时我想重新编写此文件夹以使用动态页面,例如:

if I have: 如果我有:

mydomain.com/folder
mydomain.com/folder/

and

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

then I want to open only "mydomain.com/folder" and "mydomain.com/folder/second" and this URLs should open a dynamic pages. 那么我只想打开“ mydomain.com/folder”和“ mydomain.com/folder/second”,此URL应该会打开一个动态页面。 This is what I am trying, but it does not work, it crash my page: 这是我正在尝试的方法,但是不起作用,它使我的页面崩溃:

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] 

What am I doing wrong? 我究竟做错了什么? can you help me to run the right syntax please? 您能帮我运行正确的语法吗?

You need to redirect the trailing slash URL's instead of just rewriting them. 您需要重定向尾部斜杠URL,而不仅仅是重写它们。 You also probably need to add some conditions to the rules: 您可能还需要向规则添加一些条件:

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]
RewriteEngine On
RewriteRule ^([^\/]+)/?$    includes/category.php?url=$1&pageType=category [L] 

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

you may also add the following conditions (before rewrites) to exclude real file and folders from those rule: 您还可以添加以下条件(在重写之前)以从这些规则中排除真实文件和文件夹:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM