gpt4 book ai didi

regex - .htaccess 如何删除文件扩展名和索引文件

转载 作者:行者123 更新时间:2023-12-04 08:14:02 36 4
gpt4 key购买 nike

我正在尝试通过 .htaccess 实现一些目标,但一直遇到问题。在你告诉我我需要更好地研究并且在这个或其他论坛上已经有一个解决方案之前,请知道我已经这样做了。在来这里之前,我总是尝试自己解决问题,但这真的让我很难过。我尝试过的一切都只是部分奏效。这里的任何帮助或教育将不胜感激。
我的网站具有以下简单结构:

(root)
| index.html
| .htaccess
|
|___portal-folder
| index.php
| home.php
|
|_____admin-folder
| index.php
我希望实现以下目标:
  • 当用户导航到任何基本目录时,例如 site.comsite.com/portal-folder/他们没有看到索引文件名 index.htmlindex.php在他们的浏览器中。
  • 如果用户导航到完整 URL site.com/index.html,同样适用或 site.com/portal-folder/index.php我希望用户看到 site.comsite.com/portal-folder/分别在他们的浏览器中。
  • 去除浏览器中所有文件的文件扩展名。因此,例如导航到 site.com/portal-folder/home.php将显示为 site.com/portal-folder/home在浏览器中

  • 我正在使用以下代码,但我的行为很奇怪。例如:
  • 导航至 site.com/portal-folder/index不会删除索引文件名并显示为 site.com/portal-folder/index而不是 site.com/portal-folder/在浏览器中
  • 导航至 site.com/portal-folder/不会删除索引文件名并显示为 site.com/portal-folder/index.php在浏览器中。
  • 导航至 site.com/portal-folder/index.php将用户带回根 site.com
  • 导航至 site.com/portal-folder/home工作正常,但导航到 site.com/portal-folder/home.php不会剥离 .php 扩展名。
  • 导航至 site.com工作正常,但导航到 site.com/index.html不会删除索引文件名。
    RewriteEngine On

    DirectoryIndex index.html index.php

    # remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
    RewriteRule ^(.+?)/$ /$1 [R=301,L]

    # To internally forward /dir/file to /dir/file.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+?)/?$ /$1.php [L]

  • 服务器信息:Apache 版本 2.4.46

    最佳答案

    这样做:

    DirectoryIndex index.html index.php

    RewriteEngine On

    # To externally redirect /dir/file.php to /dir/file and optionally remove index
    RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))(?:\.php|\.html)?[/\s?] [NC]
    RewriteRule ^ /%1%2 [R=301,L,NE]

    # remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^(.+)/+$
    RewriteRule ^ %1 [R=301,NE,L]

    # To internally forward /dir/file to /dir/file.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]

    关于regex - .htaccess 如何删除文件扩展名和索引文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65800452/

    36 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com