【问题标题】:htaccess redirect to index.html in same (unknown) subfolderhtaccess 重定向到相同(未知)子文件夹中的 index.html
【发布时间】:2018-08-14 12:32:34
【问题描述】:

我喜欢将所有请求重定向到放置 .htaccess 的 index.html

.htaccess 的位置:

https://example.com/unknown/whatever/.htaccess

这个请求:

https://example.com/unknown/whatever/article1

应该重定向到

https://example.com/unknown/whatever/index.html

它只对我有用,当我知道子文件夹路径时:

RewriteEngine On
RewriteBase /
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule . /unknown/whatever/index.html [L]   

知道当子文件夹路径和数量未知时如何进行这项工作吗?

【问题讨论】:

    标签: apache .htaccess url-rewriting


    【解决方案1】:

    它对我有用(请求的 URL - 未知/whatever/article1)

    RewriteEngine On
    RewriteBase /
    DirectorySlash Off
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*)$ $1/index.html [L]
    

    服务器文件结构:

    unknown/whatever/article1/index.html
    .htaccess
    

    这个也适用于我(请求的 URL - 未知/whatever/article1):

    RewriteEngine On
    RewriteBase /
    DirectorySlash Off
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule (.*)/(.*)$ $1/index.html [L]  
    

    服务器文件结构:

    unknown/whatever/index.html
    .htaccess
    

    请求:unknown/whatever/article1

    .htaccess:

    RewriteEngine On
    DirectorySlash Off
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*)$ index.html
    

    文件结构:

    unknown/whatever/index.html
    unknown/whatever/.htaccess
    

    【讨论】:

    • 这条规则似乎在htaccess.madewithlove.be 中有效,但在服务器上它向我显示了(现有)文件夹https://example.com/unknown/whatever/article1 中的文件
    • 是否将https://example.com/unknown/whatever/article1重定向到https://example.com/unknown/whatever/index.html
    • 不,它只显示文件夹'example.com/unknown/whatever/article1'。顺便说一句,如果这很重要,我的实时示例有更多的子文件夹......
    • 还有其他想法吗?它从不重定向。可能是因为该工具不支持“DirectorySlash Off”。谢谢
    • 好的,如果我将 .htaccess 放在根文件夹中,它可以工作,但如果它在 example.com/unknown/whatever/.htaccess 中则不行,我必须把它放在那里,因为规则应该只为那个文件夹设置跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 2015-05-30
    • 2021-02-19
    • 2019-08-01
    • 1970-01-01
    • 2014-06-01
    相关资源
    最近更新 更多