繁体   English   中英

Apache 上没有尾部斜杠的虚 URL

[英]Vanity URLs without trailing slashes on Apache

下面的代码将我们网站上 /profiles/ 目录中的所有 URL 从example.com/profiles/name/重写为example.com/name/ ,但我们还想删除尾部斜杠以进一步简化生成的 URL更漂亮的example.com/name就像在现代社交媒体上一样。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profiles/$1 [NC,L]

如何做到这一点(并安全地完成)? 我们已经在 Stumble On 上看到了几种解决方案,如果组合起来可能会起作用,但是我们站点上的所有配置文件目前都有自己的物理目录,而不是由脚本即时组装。

更新: @jon-lin 在How to access directory's index.php without a trailing slash AND not get 301 redirect 中提供了类似情况的解决方案——但我们没有弄清楚如何将它应用于我们的(如上所述)。

你可以尝试做

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

哪个应该适用于任何网址

使用以下重定向:

RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /profiles/$0 [NC,L]

您需要禁用目录斜杠

尝试 :

DirectorySlash Off

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profiles/$1 [NC,L]

通过添加@jon-lin 在How to access directory's index.php without a trailing slash AND not get 301 redirect (internally rewriting the trailing slash) 中建议的部分代码,我们实际上完成了这项工作:

# Vanity URLs

DirectorySlash Off

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /profiles/$1 [NC,L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/

Gucci 在 FASHION NET 上的简介(位于/profiles/gucci/ )现在可以在https://www.fashion.net/gucci访问——没有尾部斜杠! 谢谢你,@jon-lin!!!!

暂无
暂无

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

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