【问题标题】:access pages without extensions except one page htaccess访问没有扩展名的页面,除了一页 htaccess
【发布时间】:2016-10-17 15:26:54
【问题描述】:

我有一个网站,例如: https://website.com

它将页面加载为:

https://website.com/page1.html
https://website.com/page2.html
https://website.com/page3.html
https://website.com/page4.html

我想打开除了page2.html之外没有扩展名的以上页面,即:

https://website.com/page1
https://website.com/page2.html
https://website.com/page3
https://website.com/page4

如果用户明确输入:

https://website.com/page1.html

那么也应该重定向到:

https://website.com/page1

page3 和 page4 相同

我在 .htaccess 中应用了以下规则:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

它可以打开没有扩展名的页面,即:

https://website.com/page1
https://website.com/page2
https://website.com/page3
https://website.com/page4

但是当我进入时:

https://website.com/page1.html

它被打开了,但我需要的是,当我打开时:

https://website.com/page1.html

应该会自动重定向到

https://website.com/page1

这个条件应该适用于除page2.html之外的所有页面

提前感谢您的帮助。

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting url-redirection


    【解决方案1】:

    在您现有的重写之前添加类似以下内容。

    RewriteCond %{REQUEST_URI} !=/page2.html
    RewriteRule ^/?(.*)\.html /$1 [L,R=301]
    

    可能需要根据 page1/2/3/4 的具体内容进行修改。 RewriteRule 模式中的 /? 位仅用于使其对 htaccess 文件和主要 Apache 配置文件都通用。如果您只打算在 htaccess 文件中向我们提供此内容,则可以将其删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 2012-12-21
      相关资源
      最近更新 更多