尾随斜线问题 [英] Trailing slashes problem
问题描述
当我键入此http://example.com/Hello%20There/,它显示了
索引页至极为:http://example.com/Hello%20There/index.html
When I type this "http://example.com/Hello%20There/" , it displays the index page wich is : "http://example.com/Hello%20There/index.html" .
好吧,我想要做的就是当用户键入http://example.com/Hello%20There
(所以喜欢它,除了第一个不具有斜线)。
Well, what I want to do is when the user types "http://example.com/Hello%20There" (so like the first one except it doesn't have a trailing slash).
我试了很多东西,特别是常规的前pressions,但没有工作,因为我觉得
该服务器时,他发现一个空格(%20中的URL)停止了reg EXP过程。
I tried many things and specially regular expressions, but nothing works because I think that the server stops the reg exp process when he finds a space ("%20" in the URL).
我想这章EXP:
Options +FollowSymLinks
rewriteEngine On rewriteCond %{REQUEST_URI} ^(.*)\ (.*html)$
rewriteRule ^.*$ %1-%2 [E=space_replacer:%1-%2]
rewriteCond %{ENV:space_replacer}!^$
rewriteCond %{ENV:space_replacer}!^.*\ .*$
rewriteRule ^.*$ %{ENV:space_replacer} [R=301,L]
和也提出:
DirectorySlash On
阿帕奇mod_dir模块中。
in the "mod_dir" module of Apache.
所以,我的问题是:
- 如何告诉服务器添加结尾的斜线,当用户键入网址
不带后缀斜线; $
So, my question is: - How to tell to the server to add a trailing slash when the user types an url without a trailing slash;$
推荐答案
您可以让一个角色可选通过附加
量词它是这样的:
You can make a character optional by appending the ?
quantifier to it like this:
RewriteRule ^([^/]+)/?$ $1/index.html
现在这两个 / foobar的
和 / foobar的/
将被改写为 / foobar的/的index.html
。
Now both /foobar
and /foobar/
would be rewritten to /foobar/index.html
.
但它会更好,如果你只使用一个拼写,有或没有结尾的斜线,然后重定向另一个:
But it would be better if you use just one spelling, with or without the trailing slash, and redirect the other one:
# remove trailing slash
RewriteRule (.+)/$ /$1 [L,R=301]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$1/ [L,R=301]
这些规则删除或添加缺失的结尾的斜线,做一个永久重定向。
These rules either remove or add a missing trailing slash and do a permanent redirect.
这篇关于尾随斜线问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!