简体   繁体   English

从 .htaccess 配置中删除结尾的斜杠

[英]Remove ending trailing slash from .htaccess configuration

I Created a directory with name test.example.com which has index.html in directory test.example.com我创建了一个名为test.example.com的目录,它在目录test.example.com中有index.html

I have the following path on the domain:我在域上有以下路径:

http://example.com/test.example.com/

and .htaccess with following configuration和 .htaccess 具有以下配置



# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.


Options +FollowSymLinks
RewriteEngine On
RewriteBase /


RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]

<IfModule mime_module>
  AddHandler application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

upon visiting http://example.com/test.example.com/ i would like to redirect on removed trailing slash http://example.com/test.example.com hosted with index.html upon visiting http://example.com/test.example.com/ i would like to redirect on removed trailing slash http://example.com/test.example.com hosted with index.html

Well, you need an external redirection removing a potential trailing slash:好吧,您需要一个外部重定向来删除潜在的斜杠:

RewriteRule ^/?(test\.example\.com)/$ /$1 [R=301]

In case you also want to apply that rule to paths below that folder this will do:如果您还想将该规则应用于该文件夹下的路径,则可以:

RewriteRule ^/?(test\.example\.com)(.*)/$ /$1$2 [R=301]

It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up.最好先从 302 临时重定向开始,然后再将其更改为 301 永久重定向,前提是您确定一切都已正确设置。 That prevents caching issues while trying things out...这可以防止在尝试时出现缓存问题......

This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file).此实现同样适用于 http 服务器主机配置或动态配置文件(“.htaccess”文件)。 Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host.显然重写模块需要在 http 服务器内部加载并在 http 主机中启用。 In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.如果您使用动态配置文件,您需要注意它的解释在主机配置中完全启用,并且它位于主机的DOCUMENT_ROOT文件夹中。

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess").和一般性评论:您应该始终更喜欢将此类规则放在 http 服务器主机配置中,而不是使用动态配置文件(“.htaccess”)。 Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server.这些动态配置文件增加了复杂性,通常是意外行为的原因,难以调试,并且它们确实减慢了 http 服务器的速度。 They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).它们仅在您无法访问真正的 http 服务器主机配置(阅读:非常便宜的服务提供商)或坚持编写自己的规则的应用程序(这是一个明显的安全噩梦)的情况下作为最后一个选项提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM