【问题标题】:.htaccess - When there is no requested URI.htaccess - 当没有请求的 URI 时
【发布时间】:2012-12-14 16:24:23
【问题描述】:

我正在寻找一个.htaccess 文件,它可以让任何请求传递到其原始目标但重定向到特定文件夹(在这种情况下为启动屏幕),但没有指定目标。我不太熟悉.htaccess,希望能得到一些帮助。

示例:我正在请求http://www.domain.com/folder/file.php,它应该可以通过。但是如果我请求http://www.domain.com/,它应该重定向到http://www.domain.com/splash/

到目前为止,我的内容正确重定向到 /splash/,但将 一切 重定向到 /splash/

<IfModule mod_rewrite.c>
    RewriteEngine On

    # If the requested URI is empty...
    RewriteCond %{REQUEST_URI} !^$

    # ...then redirect to the "splash" folder
    RewriteRule .* splash [L]

    # Otherwise rewrite the base
    RewriteBase /

    # If the request is not a folder or a file, redirects to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

谢谢!

编辑:重要的一点是将http://www.domain.com/ 重定向到http://www.domain.com/splash/,但允许直接访问http://www.domain.com/index.php

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    %{REQUEST_URI} 变量包含一个前导斜杠,因此它永远不会为空。你可以摆脱它,只使用这条规则:

    RewriteRule ^/?$ /splash/ [L,R]
    

    如果您希望浏览器地址栏中显示的 URL 保留为 http://www.domain.com/,请从方括号中删除 ,R[L]

    【讨论】:

    • 它部分工作。我现在可以访问domain.com/folder,但domain.com/index.php 仍在重定向到domain.com/splash
    • @RémiBreton index.php 不应该匹配 ^/?$,您确定这不是浏览器缓存问题吗?
    • 阳性。清除缓存、多个浏览器、多台机器,结果都一样:直接转到/splash/。感谢您的帮助!
    • @RémiBreton /splash/ 文件夹真的存在吗?还是通过 index.php 路由?
    • @RémiBreton 那么不知道,我有上面的规则(没有R),然后是# Otherwise rewrite the base之后的htaccess文件中的所有内容,在新安装的空白htaccess文件中apache 2.2,创建了一个“splash”目录,当我请求/ 时会显示“splash”的内容,并在我请求/index.php 或其他任何内容时获取index.php 文件,例如/asdashfakfh。所以它对我有用。
    猜你喜欢
    • 2017-03-10
    • 2016-07-31
    • 2013-06-22
    • 2014-05-20
    • 2012-03-13
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多