自动拖尾斜线

时间:2015-01-09 03:51:07

标签: .htaccess

嗨朋友我正在添加尾部斜杠并删除网址末尾的扩展名,通过以下ht访问条件它工作正常我的问题是如果我手动键入网址而不拖尾斜线它不会自动添加尾部斜杠例如如果我输入以下网址

www.example.com/project/product

它不会自动添加trailng斜杠 我的访问条件是

Options -Indexes
Options +FollowSymLinks +MultiViews
RewriteEngine on

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

# disable directory browsing
RewriteCond %{THE_REQUEST} ^.+?\ [^?]+\.php[?\ ]
RewriteRule \.php$ - [F]

RewriteCond %{THE_REQUEST} ^.+?\ [^?]+\.html[?\ ]
RewriteRule \.html$ - [F]

ErrorDocument 403 http://www.example.com/
ErrorDocument 404 http://www.example.com/

如果输入手动网址

,请告诉我添加自动尾随斜杠的指导

1 个答案:

答案 0 :(得分:0)

这是因为您依赖于多视图,这是由不同的模块处理的。此模块在 mod_rewrite之前处理请求,这意味着您的规则永远不会得到处理。为此,您需要关闭多视图并自行处理映射:

Options -Indexes
Options +FollowSymLinks -MultiViews
RewriteEngine on

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

# Handle the php mapping
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRUle ^(.*?)/?$ /$1.php [L]

# Handle the html mapping
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRUle ^(.*?)/?$ /$1.html [L]

# disable directory browsing
RewriteCond %{THE_REQUEST} ^.+?\ [^?]+\.php[?\ ]
RewriteRule \.php$ - [F]

RewriteCond %{THE_REQUEST} ^.+?\ [^?]+\.html[?\ ]
RewriteRule \.html$ - [F]

ErrorDocument 403 http://www.example.com/
ErrorDocument 404 http://www.example.com/