【问题标题】:Make htaccess rewrite rules check for index page in that folderpath first首先让 htaccess 重写规则检查该文件夹路径中的索引页
【发布时间】:2014-11-21 20:35:15
【问题描述】:

这可能是一个非常简单的问题。我只是有一个简单的干净 URL 类型的设置,所以 www.website.com/articles/music 会将其重写为 index.php?cat=articles&subcat=music 或其他什么,这只是一个示例。但当然,如果它是一个文件请求(例如website.com/images/icons/music.png),它会首先检查一个有效的文件。这很容易,网上有很多使用 .htaccess 和重写规则的教程,它们给了我类似的东西

RewriteEngine On
# Check for existing files first
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Set base
RewriteBase /
# Rewrite rules not shown but would be below

我想要做的最后一个条件是它还检查给定的 URL 文件夹路径是否存在并且其中是否有 index.php 或 index.html。例如。继续第一个示例,检查以确保不存在 website.com/articles/music/index.htmlindex.php,如果是,则重定向到它而不是重写规则。

就像我说的可能很简单,但我不擅长 htaccess 的东西,因为我很少使用它,而且这是一个我找不到答案的不规则问题。并不是说它们可能不存在,而是我尝试使用的措辞没有弹出任何内容。谢谢。

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite clean-urls


    【解决方案1】:

    您需要RewriteCond 来检查 URI 文件夹中是否存在 `index.html(或 php):

    RewriteEngine On
    # Set base
    RewriteBase /
    
    # Check for existing files first
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    # check for index.php
    RewriteCond %{DOCUMENT_ROOT}/$1/index\.php -f [NC]
    RewriteRule ^(.+?)/?$ $1/index.php [L]
    
    # check for index.html
    RewriteCond %{DOCUMENT_ROOT}/$1/index\.html -f [NC]
    RewriteRule ^(.+?)/?$ $1/index.html [L]
    
    # route to default /imdex.php
    RewriteRule . index.php [L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多