重定向到斜杠 (htaccess) [英] Redirect to trailng slash (htaccess)

查看:29
本文介绍了重定向到斜杠 (htaccess)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试为每个链接添加尾部斜杠.即 http://mysite.com/products 应该使 301 重定向到 http://mysite.com/products/ 等等.但是如何?这是 htaccess:

Trying to add trailing slash to every link. i.e. http://mysite.com/products should make 301 redirect to http://mysite.com/products/ etc. But how? Here is htaccess:

RewriteEngine on
DirectoryIndex index.php
Options -Indexes
RewriteBase /

RewriteCond %{REQUEST_URI} \.css$
RewriteCond %{QUERY_STRING} ^pack$
RewriteRule ^(.*)$ /modules/system/css_compactor.php?filename=$1 [L]
RewriteCond %{REQUEST_URI} \.js$
RewriteCond %{QUERY_STRING} ^pack$
RewriteRule ^(.*)$ /modules/system/js_compactor.php?filename=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
RewriteRule /admin/(.*)$ /admin/index.php

需要帮助!

推荐答案

这是我正在使用的

RewriteEngine on
RewriteBase /

### CHECK FOR TRAILING SLASH - Will ignore files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]

基本上,这可确保它不会向文件添加尾随,而只会向文件夹或路径添加.

Basically, this makes sure that it doesn't add a trailing to file and only folders or paths.

编辑

使其与域无关

RewriteRule ^(.*)$ $1/ [L,R=301]

这篇关于重定向到斜杠 (htaccess)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆