【问题标题】:.htaccess - redirecting if file or directory not exists [duplicate].htaccess - 如果文件或目录不存在则重定向[重复]
【发布时间】:2016-02-28 22:12:24
【问题描述】:

我有一个简单的 CMS 文件结构:

application/
data/
src/
web/
.htaccess
index.php

我想将所有请求重定向到/index.php,除非文件或目录存在,然后我想将所有请求重定向到/web。 例子: http://localhost.com/news/example-news 到 index.php/ http://localhost.com/images/logo.png 到 web/images/logo.png

现在我的 .htaccess 看起来:.htaccess

【问题讨论】:

  • 不,因为如果文件存在,我需要将目录 /web 设置为主目录
  • 您应该始终将代码粘贴到问题中,而不是链接到它。尽可能少使用外部网站。

标签: php .htaccess mod-rewrite redirect


【解决方案1】:

试试这个...

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ /web [L]

RewriteRule ^.*$ index.php [L]

第一条规则将捕获任何可以映射到现有文件或目录并重写到 Web 文件夹的请求。第二条规则(没有 rewritecond)将捕获剩余的并重写 index.php

【讨论】:

    【解决方案2】:

    您只需将代码更改为如下所示。

    改一下

    RewriteCond %{REQUEST_URI} !^/index.php
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ index.php [NC,L]
    RewriteRule ^(/)?$ index.php [L]
    

    到这里

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{REQUEST_URI} !^/index.php
    RewriteRule ^(.*)$ /web/index.php [L]
    
    RewriteRule ^(.*)$ index.php [NC,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-12
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      相关资源
      最近更新 更多