url上的尾部斜杠不会加载页面

时间:2013-11-14 22:28:48

标签: .htaccess url-rewriting

当我导航到example.com/books(books.php)时,页面加载了预期的脚本,但是当我添加斜杠example.com/books/时,页面没有加载所需的脚本,我需要什么要改变以确保它加载有和没有斜线?

以下是我的.htaccess的内容:

RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png|js|ico|svg)$ - [L]
RewriteCond %{REQUEST_URI} "/folder/" [OR]
RewriteCond %{REQUEST_URI} "/folder2/" [OR]
RewriteCond %{REQUEST_URI} "/folder3/"
RewriteRule (.*) $1 [L]
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php
RewriteRule ^([A-Za-z0-9-]+)/?$ book.php?slug=$1 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/(.*)/? read.php?slug=$1&cslug=$2 [NC,L]

1 个答案:

答案 0 :(得分:1)

更改此部分:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

以便执行额外检查以删除任何尾部斜杠。

RewriteCond %{REQUEST_FILENAME}\.php -f
如果您拥有URI /book/

条件将失败,因为/book/.php不存在。

尝试:

RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]