当文件不存在的.htaccess重定向 [英] .htaccess redirecting when file doesn't exists

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

问题描述

我使用的.htaccess将用户重定向到我的主控制器,这是工作的罚款。但是,当我调用一个js文件不存在这样的:

 <脚本的src =/ JS / jquery1231231312.js>< / SCRIPT>
 

而不是

只是说404 - 文件不存在,这个js文件越来越index.php文件的内容。我该如何操作?

这是我的.htaccess

 < IfModule mod_rewrite.c>
    RewriteEngine叙述上
    的RewriteCond%{} REQUEST_FILENAME!-d
    的RewriteCond%{} REQUEST_FILENAME!-f
    重写规则^(。*)$的index.php?URL = $ 1 [QSA,L]
< / IfModule>
 

解决方案

嗯,那你有它设置的方式,一切都被通过路由的index.php 。如果一个请求是为不存在的资源,它通过被路由的index.php 。它是由index.php来实现的东西是不存在,并返回404错误,但我猜测它的设置,这样,如果 URL = 不存在,以返回主页。

您可以更改您的规则,忽略了路由的某些文件,像你的例子:

  RewriteEngine叙述上
的RewriteCond%{REQUEST_URI}!^ / JS /
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^(。*)$的index.php?URL = $ 1 [QSA,L]
 

这将使它所以没有在 / JS 目录将永远打不通路由的index.php ,所以如果有人要求在 / JS 目录中不存在的文件,他们就会拿到常规404响应。


编辑:

  

这就是我想要的,忽略了路由的某些文件(JS,CSS,图像),因为这时如果文件不存在,我会得到正常的错误!

如果你想忽略所有的图像,JavaScript和CSS,那么2号线应该是:

 的RewriteCond%{REQUEST_URI} \!(PNG | JPE G |?GIF | CSS | JS)$ [NC]
 

I'm using .htaccess to redirect users to my main controller and it is working fine. But when i call a js file that doesn't exist like:

<script src="/js/jquery1231231312.js"></script>

Instead of just says 404 - file doesn't exists, this js file is getting the content of index.php. How should i proceed?

This is my .htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

解决方案

Well, the way that you have it setup, everything gets routed through index.php. If a request is made for a resource that doesn't exist, it gets routed through index.php. It's up to index.php to realize something's not there, and return a 404 error, but I'm guessing it's setup so that if the url= doesn't exist, to returns the home page.

You can change your rules to ignore the routing for certain files, like for your example:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/js/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

This would make it so nothing in the /js directory will ever get routed through index.php, so if someone requests a non-existing file in the /js directory, they'll just get the regular 404 response.


EDIT:

That's what I want, to ignore the routing for certain files (js, css, images), because then if the files doesn't exists i would get the normal error!

If you want to ignore all images, javascript, and css, then the 2nd line should be:

RewriteCond %{REQUEST_URI} !\.(png|jpe?g|gif|css|js)$ [NC]

这篇关于当文件不存在的.htaccess重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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