dtj2ww9500 2016-12-14 11:06
浏览 32
已采纳

Htaccess索引文件,如果存在

I would need such a .htaccess file, which enables me direct access to some files by extension (images), and in any other case it checks if entryPoint2.php exists, if does, execute that file, if not, executes entryPont.php. This is what I made so far:

Options +FollowSymLinks
Options -Indexes

RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.ico|\.flv|\.mpeg|\.mp4|\.mp3|\.swf)$
RewriteCond %{REQUEST_URI} !^/forum
RewriteCond %{REQUEST_URI} !^/assets/downloads
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/entryPoint2.php -f
RewriteRule ^(.*)$ entryPoint2.php [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ entryPoint.php [QSA]
RewriteRule ^robots\.txt$ http://www.example.com [R,NE,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST} [L,R=301]
<Files .htaccess>
Order Allow,Deny
Deny from All
</Files>

images works, but I get "forbidden" error, it looks for index.php

  • 写回答

1条回答 默认 最新

  • dongwei2610 2016-12-16 16:01
    关注

    Your requirement turns out to be less complex than I initially thought. What you need is a white list and a simple file check.

    This should do:

    RewriteEngine on
    RewriteRule \.(png|jpg|gif|jpeg|bmp|ico|flv|mpeg|mp4|mp3|swf)$ - [L]
    RewriteCond %{DOCUMENT_ROOT}/entryPoint2.php -f
    RewriteRule ^ entryPoint2.php [L]
    RewriteRule ^ entryPoint.php
    

    Line by line explanation:

    • Line 1 is self-explanatory.
    • Line 2 is an extension-based while list. It makes Apache serve files matching those extensions immediately, ignoring the rest of the .htaccess.
    • Line 3 checks the existence of entryPoint2.php. If, for instance, DOCUMENT_ROOT is /foo/bar, it checks if /foo/bar/entryPoint2.php exists.
    • Line 4 will serve entryPoint2.php if line 3 evaluates to true, ignoring the rest of the .htaccess.
    • Line 5 serves entryPoint.php for any other case.

    There you go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog