URL 域后重写和双斜线:如何使它成为单斜线?

URL rewriting and double slash after domain : how to make it single slash?

我想编写一条规则,将所有页面从 https://dev.example.com 重定向 (301) 到 https://www.example.com

dev.domain.com 的根目录中,在我的 .htaccess 中我放了这一行:

RewriteRule ^/(.*)$ https://www.example.com/ [R=301,L]

重定向有效,但所有子页面都有双斜杠。

示例:https://dev.example.com/contact.html 重定向到 https://www.example.com//contact.html

这是行不通的! 我该如何解决这个问题?

您能否尝试使用您展示的示例进行跟踪、编写和测试。请在测试您的 URL 之前清除您的浏览器缓存。此外,这些规则将查找在其 HTTP_HOST 可变部分中没有 www 的 URL,然后将删除带有 www 和其余部分的域名(最开始的部分直到 . 第一次出现)域名部分。

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?!www\.)(?:[^.]*\.)(.*)
RewriteRule ^(.*)$ https://www.%1%{REQUEST_URI} [NE,L]