.htacess如果文件不存在,则执行重写 [英] .htacess to perform rewrite if file does not exist

查看:98
本文介绍了.htacess如果文件不存在,则执行重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个单独的.htaccess文件,我希望将它们融合在一起,以便始终优先执行第一次重写,这会将一些流量重定向到基于该域的https。然后,如果网址中的文件不存在,则将流量发送到php文件。这是一个网址缩短器,但是如果返回404,则显示404错误页面。

I have two separate .htaccess files that I'd like to be fused together so that the first rewrite always takes precedence, which redirects some traffic to https base which domain. Then if the file at the url does not exist, then it sends the traffic to a php file. It is a url shortener, but if that returns at 404, then it shows a 404 error page.

以下是.htaccess文件的详细内容:

Here are the bits and pieces of the .htaccess files:

下面,我相信应该将除kore.tt和korett.com以外的所有http流量重定向到https:

This, below, I believe should redirect all http traffic except kore.tt and korett.com to https:

RewriteEngine on 
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)?(kore\.tt|korett\.com) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

这是来自网址缩短程序的东西,应该将不存在的流量发送到loader.php

This is something from the url shortener that is supposed to send traffic that doesn't exist to loader.php

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /loader.php [L]
</IfModule>

但是如果返回404错误。这就是简单的404错误捕获。

But then if that returns a 404 error. Then this is the simple 404 error catch.

ErrorDocument 404 /404.html


推荐答案

您可以在htaccess中使用条件。还有许多创建规则的方法。看看是否可以为您工作。

You can use a condition in your htaccess. There are many more ways to create rules. See if this can work for you.

  #check to see if loader.php exists on the filesystem, then do rewrite
  <If "-f %{DOCUMENT_ROOT} . '/loader.php'">
     RewriteEngine On
     RewriteBase /
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^.*$ /loader.php [L]
  </If>
   #otherwise redirect to 404 page
  <Else>
     RewriteEngine On
     RewriteRule ^ /404.html [R=404,L]
  </Else>

这篇关于.htacess如果文件不存在,则执行重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆