.htaccess 如何通过htaccess删除域名后的双斜杠?

kxkpmulp  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(131)

这是我的. htaccess。

DirectorySlash Off

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.php\ HTTP/
RewriteRule ^(.*)\.php$ /$1 [R=301,L]

# Ensure www. and https
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Rewrite the URL to append a trailing slash to directories (internally)
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*[^/])$ $1/ [L]

虽然这似乎是正常工作,而在网络浏览器上冲浪,问题出现时,我们的SEO团队爬网使用第三方工具,如尖叫青蛙。网址出现如下:

*.mywebsite.com
*.mywebsite.com/blogs
*.mywebsite.com/blogs/blog-1
*.mywebsite.com/about-us
....
*.mywebsite.com//
*.mywebsite.com//blogs
*.mywebsite.com//blogs/blog-1
*.mywebsite.com//about-us
....

我不想两个斜杠后域的网址。我尝试了许多解决方案,可在互联网上,但他们都没有工作。

pkmbmrz7

pkmbmrz71#

您可以使用此重定向规则从URL中的任何位置删除//

RewriteEngine On

# remove multiple slashes from URL (Better)
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule .* /$0 [R=302,L,NE]

正则表达式模式\s[^?]*//在匹配THE_REQUEST变量(表示原始Apache请求)中的?(查询字符串的stat)之前匹配//
请注意,默认情况下,RewriteRule中的模式通过Apache mod_rewrite引擎去除了多个/

相关问题