【问题标题】:How to add slash at the end of the URL?如何在 URL 末尾添加斜杠?
【发布时间】:2019-11-07 05:15:06
【问题描述】:

我的 .htaccess 文件中给出了以下规则,一切正常,只有一个例外:

#Active mod_rewrite
RewriteEngine on

#Name of APP
RewriteBase /web

#DirectorySlash Off

Options -MultiViews

#Remove web/ from URLs using a redirect rule
RewriteCond %{THE_REQUEST} \s/+(.+/)?web/(\S*) [NC]
RewriteRule ^ /%1%2? [R=301,L,NE]

#Disable WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
#Active HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} assets/(css|fonts|js|img)/(.+)$
RewriteRule ^(.*)$ assets/%1/%2 [L,R]

#Add slash at the end (/)
#RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

RewriteRule ^([a-zA-Z0-9-/]+)$ product.php?id=$1

该异常的错误是,当我尝试在 URL 中始终启用斜杠/结尾时,通过:

RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

而且,这样做会丢失文件排除规则

RewriteCond %{REQUEST_URI} assets/(css|fonts|js|img)/(.+)$ 
RewriteRule ^(.*)$ assets/%1/%2 [L,R]

而且,更糟糕的是,当我尝试访问产品时,它会在 URL 中显示此内容

https://example.org/product.php/?id=mi-producto-de-url-amigable/?

一团糟,我已经审查了一些文章,也是通过这种方式,我尝试了每个帖子中建议的替代方案 RewriteRule ^(.*[^/]$) $1/ [R,QSA,L] 和其他但没有成功。

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    Add Trailing Slash .htaccess

    你试过了吗?

    RewriteCond %{REQUEST_URI} !(/$|\.) 
    RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] 
    

    【讨论】:

    • 它似乎有效,我正在通过手机进行测试,如前所述,它可以正常工作;)
    • 所以现在我的问题将是重复的 :( 或者我可以接受你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2021-03-09
    • 2018-09-18
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    相关资源
    最近更新 更多