apache 尾随斜线问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2006186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trailing slashes problem
提问by Zakaria
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/”时,它显示的索引页面是:“ http://example.com/Hello%20There/index.html”。
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).
好吧,我想要做的是当用户输入“ http://example.com/Hello%20There”(所以就像第一个一样,只是它没有尾部斜杠)。
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).
我尝试了很多东西,特别是正则表达式,但没有任何效果,因为我认为服务器在找到空格(URL 中的“%20”)时会停止 reg exp 进程。
I tried this reg 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]
and also put:
并放:
DirectorySlash On
in the "mod_dir" module of Apache.
在 Apache 的“mod_dir”模块中。
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;$
所以,我的问题是: - 当用户键入一个不带斜杠的 url 时,如何告诉服务器添加一个斜杠;$
回答by Gumbo
You can make a character optional by appending the ?quantifier to it like this:
您可以通过将?量词附加到字符来使字符可选,如下所示:
RewriteRule ^([^/]+)/?$ /index.html
Now both /foobarand /foobar/would be rewritten to /foobar/index.html.
现在,/foobar和/foobar/都将被重写为/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 (.+)/$ / [L,R=301]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ // [L,R=301]
These rules either remove or add a missing trailing slash and do a permanent redirect.
这些规则删除或添加缺少的尾部斜杠并执行永久重定向。
回答by John K
I had the same problem, but I was using mod_alias to set up a subsite. Turns out, I needed to make a second alias without the trailing slash so that it would work correctly. Looked something like this:
我遇到了同样的问题,但我使用 mod_alias 来设置子站点。事实证明,我需要制作第二个不带斜杠的别名才能正常工作。看起来像这样:
Alias /forum/ "/var/www/forum"
Alias /forum "/var/www/forum"
<Directory "/var/www/forum">
Options FollowSymlinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
In Ubuntu, I had to edit the /etc/apache2/mods-enabled/alias.conffile with these lines, then restart apache. Couldn't find this answer anywhere on the web; I just stumbled onto it myself as mod_rewrite wasn't working and the DirectorySlash command didn't help either. I was appending a non-Drupal program as a subsite under a Drupal installation, which is what kicked off all this madness in the first place...
在 Ubuntu 中,我必须用这些行编辑/etc/apache2/mods-enabled/alias.conf文件,然后重新启动 apache。无法在网络上的任何地方找到此答案;我自己偶然发现了它,因为 mod_rewrite 不起作用并且 DirectorySlash 命令也没有帮助。我在 Drupal 安装下附加了一个非 Drupal 程序作为子站点,这首先引发了所有这些疯狂......
回答by TIGER.MY
Don't use trailing slash to define an alias.
不要使用尾部斜杠来定义别名。
Both URLs http://example.com/myalias1and http://example.com/myalias1/would work fine.
URL http://example.com/myalias1和http://example.com/myalias1/都可以正常工作。
Example:
例子:
sudo vi /etc/apache2/apache2.conf
Alias /myalias1 "/path/to/folder1"

