重写以将尾部斜杠重定向到非尾部斜杠

时间:2017-01-24 09:54:22

标签: php apache .htaccess mod-rewrite

我有一个网站,可在medify.eumedify.eu/nl上找到荷兰语版本。目前,谷歌正在链接到medify.eu/nl/(最后用斜杠),这会产生错误,所以我想将medify.eu/nl/重定向到medify.eu/nl

我的.htaccess看起来像这样:

RewriteEngine On

    RewriteCond %{HTTPS} off
    # First rewrite to HTTPS:
    # Don't put www. here. If it is already there it will be included, if not
    # the subsequent rule will catch it.
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # Now, rewrite any request to the wrong domain to use www.

    AddType audio/aac .aac
    AddType audio/mp4 .mp4 .m4a
    AddType audio/mpeg .mp1 .mp2 .mp3 .mpg .mpeg
    AddType audio/ogg .oga .ogg
    AddType audio/wav .wav
    AddType audio/webm .webm
    AddType video/mp4 .mp4 .m4v
    AddType video/ogg .ogv
    AddType video/webm .webm

关注this answer我添加了两行,以便整个文件现在如下所示:

RewriteEngine On
# This will enable the Rewrite capabilities

    RewriteCond %{HTTPS} off
    # First rewrite to HTTPS:
    # Don't put www. here. If it is already there it will be included, if not
    # the subsequent rule will catch it.
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # Now, rewrite any request to the wrong domain to use www.

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

    AddType audio/aac .aac
    AddType audio/mp4 .mp4 .m4a
    AddType audio/mpeg .mp1 .mp2 .mp3 .mpg .mpeg
    AddType audio/ogg .oga .ogg
    AddType audio/wav .wav
    AddType audio/webm .webm
    AddType video/mp4 .mp4 .m4v
    AddType video/ogg .ogv
    AddType video/webm .webm

但它仍然没有将medify.eu/nl/重定向到medify.eu/nl。我有什么想法吗?

1 个答案:

答案 0 :(得分:1)

要使用.htaccess删除尾部斜杠,您可以使用:

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

确保在测试之前清除缓存。在使用您的网站测试后,输出结果为:

Removed Trailing Slash