Php .htaccess用/结尾重写

Php .htaccess用/结尾重写,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,所以我在我的.htaccess中有以下代码 Options +FollowSymlinks RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([\w\d~%.:_\-]+)$ category.php?id=$1 [NC] 这会更改url:www.domain.com/category.php?id=uncategorized 到

所以我在我的.htaccess中有以下代码

 Options +FollowSymlinks
 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^([\w\d~%.:_\-]+)$ category.php?id=$1 [NC]
这会更改url:www.domain.com/category.php?id=uncategorized

www.domain.com/uncategorized


然而,当我在结尾用斜杠进入时,它说页面不存在。有人知道我如何使新URL能够工作,无论它是否以斜杠结尾,以及其他类似的内容吗?

您可以将尾部斜杠设置为可选并添加QSA标志:


这将把末尾没有斜杠的url重定向到末尾有斜杠的url

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

这太棒了!有没有办法让它总是以斜线结尾?也许用斜杠将URL重定向到always?似乎有问题。现在我所有的链接、图像和CSS文件都有了它们的文件名。例如,如果我转到domain.com/category,我的所有链接都将类似于domain.com/category/index.php,而它应该是domain.com/index.php。换句话说,我认为文件名已经添加到每个链接的基本URL中。在这些规则中,我没有在URI中添加index.php,所以我确信这是在这些规则之外发生的。若你们注释掉尾随的斜杠规则,它工作正常吗?不,不,你们并没有添加index.php。但是如果我在页面的某个地方有一个链接index.php作为html链接,它会将文件名添加到其中。因此,如果我转到domain.com/filename,页面上的index.php链接将变成/filename/index.php。因此,如果我单击index.php,它将带我到/filename/index.php,这是错误的,因为您使用的是相对URL。只需在css、js、图像文件中使用绝对路径,而不是相对路径。这意味着您必须确保这些文件的路径以http://或斜杠开头。
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]