【问题标题】:htaccess to resolve a trailing slash to .html pagehtaccess 将斜杠解析为 .html 页面
【发布时间】:2016-02-08 17:22:11
【问题描述】:

如果有人删除扩展名并添加尾部斜杠,是否可以将页面解析为等效的 .html?

http://www.example.com/page.html

http://www.example.com/page/ 解析到 page.html。

但是,我希望尾部斜杠是可选的,而不是我在 htaccess 中添加的内容。

RewriteEngine on

# render .php as .html
RewriteRule ^(.*)\.html$ $1.php [nc]

# don't need extension on the end
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.html [NC,L]

# .php to .html
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1.html [L,R=301]

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    像这样:

    RewriteEngine On
    
    # .php to .html
    RewriteCond %{THE_REQUEST} \ /(.+)\.php[?\s] [NC]
    RewriteRule ^ /%1.html [L,R=301]
    
    # don't need extension on the end
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
    
    RewriteRule ^(.+)\.html/?$ $1.php [L,NC]
    

    确保在测试此更改之前清除浏览器缓存。

    【讨论】:

    • 完美,谢谢。 example.com/page 现在解析为 html 页面。我还需要添加什么来解决example.com/page.html/(带有.html的斜线)
    • 有道理,我看到你刚刚添加了斜杠。非常感谢。
    • 我还有一个问题。我将非 www 转发到 www,将非 http 转发到 https。它工作正常,但谷歌警告“避免登陆页面重定向”有没有办法结合? RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L] RewriteCond %{HTTP_HOST} ^example.com [NC] RewriteRule ^(.*)$ example.com/$1 [L,R=301,NC]
    • 无论如何更新上述内容以反转不需要html扩展?如果没有,然后 301 转发?谢谢!
    【解决方案2】:

    要使 html 请求的尾部斜杠可选,您可以使用:

    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^([^.]+)/?$ $1.html [NC,L]
    

    【讨论】:

    猜你喜欢
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 2021-06-29
    相关资源
    最近更新 更多