【问题标题】:.htaccess trouble with hiding file extension and forcing trailing slash.htaccess 隐藏文件扩展名和强制斜杠的问题
【发布时间】:2013-11-03 01:50:23
【问题描述】:

当我键入 domain.com/page.php 时,它不会自动更改为 domain.com/page/。此外,对于子目录中的页面,如果我点击指向 domain.com/sub/1/ 的链接,一切都很好,但如果我在浏览器中键入它,它会重定向到 domain.com/1/

这就是我在 .htaccess 中的内容

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

我应该如何处理子目录和根目录中的 htaccess 文件?我是新手。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    改变规则的顺序。

    试试这个:

    RewriteEngine on
    
    # Forces a trailing slash to be added
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+?[^/])$ /$1/ [R=301,L]
    
    # .php ext hiding
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+?)/?$ /$1.php [L]
    

    【讨论】:

      猜你喜欢
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 2018-04-09
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 2010-11-07
      相关资源
      最近更新 更多