【问题标题】:URL rewrite with htaccess to get trailing /使用 htaccess 重写 URL 以获取尾随 /
【发布时间】:2015-06-02 07:45:29
【问题描述】:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

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

## hide .php extension
# To externally redirect /dir/foo.html to /dir/foo

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_FILENAME}.html [L]

我按照Removing .php file extension with .htaccess file 中的建议使用上面的代码 sn-p 进行 URL 重写。但我无法将培训 / 添加到 URL

例如 www.somewebsite.com/contact 到 www.somewebsite.com/contact/

我要纠正什么?

【问题讨论】:

    标签: html .htaccess rewrite


    【解决方案1】:

    这样吧:

    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^index\.html$ / [R=301,L]
    
    RewriteRule ^(.+?)/index\.html$ $1/ [NE,R=302,L]
    
    ## hide .php extension
    # To externally redirect /dir/foo.html to /dir/foo    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
    RewriteRule ^ %1/ [R,L,NC]
    
    # add trailing extension
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
    RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=302,NE]
    
    ## To internally redirect /dir/foo to /dir/foo.html
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^ %{REQUEST_FILENAME}.html [L]
    

    【讨论】:

    • 没做任何改动也没加训练斜线,还是一样。
    • 添加了训练斜线,但也破坏了 js、css 和图像的所有其他链接。比如“somewebsite.com/images/logo.png
    • 这是由于您使用了相对路径。为此,您可以将其添加到页面 HTML 的 <head> 部分:<base href="/" />
    • 是的,已修复。谢谢@anubhava
    猜你喜欢
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    相关资源
    最近更新 更多