【问题标题】:Website URL should not open with .html extension [duplicate]网站 URL 不应以 .html 扩展名打开 [重复]
【发布时间】:2020-09-28 05:37:32
【问题描述】:

我想在 URL 中隐藏 .html 扩展名,我通过编辑 .htaccess 文件来做到这一点。如果我单击任何页面,则它会在没有 .html 扩展名的情况下打开,但是当我手动输入带有 .html 扩展名的 url 时,它也会打开该页面,并且 .html 扩展名会出现在 URL 中。

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php71” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php71 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

我的要求是即使我在 url 中输入 .html 也不允许打开页面。或者它应该在用户输入带有 .html 扩展名的 URL 时自动重定向。

不知道上面的代码是否正确。请帮我修复它。 网址:https://drkrinitamotwani.com/

【问题讨论】:

    标签: html .htaccess url redirect


    【解决方案1】:

    我玩了一会儿,得到了以下 .htaccess 配置:

    这将从 URL 中删除 .html,并确保在没有 .html 的情况下访问 URL 时加载 .html 文件

    你可以在这里测试它:http://martinsbh.com.au/test.html

    RewriteEngine on 
    RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
    RewriteRule ^ /%1 [NC,L,R]
    
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^ %{REQUEST_URI}.html [NC,L]
    
    RewriteEngine on
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
    
    # php -- BEGIN cPanel-generated handler, do not edit
    # Set the “ea-php71” package as the default “PHP” programming language.
    <IfModule mime_module>
    AddHandler application/x-httpd-ea-php71 .php .php7 .phtml
    </IfModule>
    # php -- END cPanel-generated handler, do not edit
    

    【讨论】:

    • 我以前这样用过这个,但是没有用:RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1。 html [NC,L]
    • 这和我发的不一样
    • 如果用户输入带有 .html 扩展名的 URL,我也想强制重定向。它应该重定向到没有 .html 扩展名的 URL
    • 应该这样做
    • 在第 7 行的第二个和第三个块之间添加这一行“Redirect 301 /index /”,不带引号。
    猜你喜欢
    • 2012-11-30
    • 2017-02-24
    • 2014-03-15
    • 2019-04-21
    • 1970-01-01
    • 2017-05-08
    • 2022-03-04
    • 2017-01-14
    • 1970-01-01
    相关资源
    最近更新 更多