大网数据广告活动
网站运营与故障问答

Point all to index.php conflict with a private directory

· 52CE 我爱测

Here is my website structure (root): private public .htaccess index.php The htaccess file contains: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ /index.php [L] Having this said, all URLs point to index.php I…

问题原文

原帖链接 · Andrei · CC BY-SA 4.0

Here is my website structure (root):

private
public
.htaccess
index.php

The htaccess file contains:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [L]

Having this said, all URLs point to index.php

In the private folder I also have a .htaccess file which contains:

Order Deny,Allow
Deny from all

The problem is that http://www.example.com/private/ throws 403 and I don't want this. I may want to use http://www.example.com/private/ location to show something on my website.

I must mention that I don't want to touch the .htaccess file from the private folder in any way.

So, I'm looking for a way to set some rules in the main .htaccess file.

How do I do that? Is it possible?

Edit: More about my problem.

Imagine a CMS with a public folder and a private folder. The private folder doesn't even belong to the root webserver. It should stay out of public_html, www or other public folders. Well, talking about a CMS, not all users understand this, and they may still keep the private folder in public_html.

Having this said, the private folder should contain a htaccess file which Deny's access to the files through the browser (example www.example.com/private/internal_script.php).

But I still want to be able to serve content in the browser for www.example.com/private/ location.

Right now I can serve what content I want on any URL. This is because of the main htaccess file which let me choose what to show using PHP. But, I still can't serve content to URL www.example.com/private/, because this points to the private directory which contains the htaccess with Deny access.

www.example.com/hello/ can be category, an article etc

www.example.com/awesome/ can be category, an article etc

www.example.com/private/ can't be nothing. I get 403...

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

This is technically possible, however, there is another "preferred" solution for "a CMS with a public folder and a private folder" (see below).

To summarise the requirements:

  1. Allow requests for /private/ or /private/<something> (where <something> is not a physical file in the /private subdirectory) to be routable through the front-controller (ie. /index.php in the root). Just like any other (non-file) URL.
  2. Any HTTP requests for /private/<actual-file> need to be blocked (403 Forbidden), as they currently are. (UPDATE: Although this is not required it seems and should be rewritten to the front-controller as in #1)
  3. Only the parent .htaccess file in the document root can be changed.

We basically need to "override" the 403 Forbidden response for the protected subdirectory when either the directory itself is requested or a non-file in that subdirectory is requested.

Ordinarily, directives in the subdirectory .htaccess file override directives in the parent .htaccess file, so we need to change the order of processing and make sure that the necessary "overriding" directives in the parent .htaccess are processed later. This can be achieved by wrapping the directives in a <Files> (or <FilesMatch>) container or in an <If> block using an Apache expression.

However, an added complication in this particular case is the use of the deprecated (Apache 2.2 and earlier) Order and Deny directives in the protected subdirectory. (On Apache 2.4, which I assume is what you are using, you should be using the single Require all denied directive instead.) In order to "override" the authorisation directives in the subdirectory you must use the same authorisation method. (Which is why the Apache docs stress that you should avoid mixing the two (old and new) authorisation methods. The old/deprecated directives are provided for backwards compatibility only and should be migrated - in their entirety - at the earliest opportunity. Being officially deprecated, they will be removed entirely in future versions of Apache.)

So, you can do something like the following in the root .htaccess file. The order of the directives does not strictly matter, you can place it before or after your mod_rewrite front-controller, however, it would be more logical to have it before.

# For any request that starts "/private" and does not map to a physical file then...
<If "%{REQUEST_URI} =~ m#^/private# && !-f %{REQUEST_FILENAME}">
    DirectoryIndex disabled
    DirectorySlash Off

    # If using Apache 2.4 directives...
    #Require all granted

    # If using old Apache 2.2 directives in the subdirectory then need to use the same here...
    Order Deny,Allow
    Allow from all
</If>

It is necessary to disable the DirectoryIndex for the subdirectory to prevent mod_dir trying to serve the index document (eg. index.php) when requesting the bare directory. If DirectoryIndex is enabled then... if the index document exists then mod_dir issues an internal subrequest which then gets blocked and the URL-path is not accessible to your CMS. If the index document does not exist then mod_dir blocks the request and the URL-path is not accessible to your CMS.

DirectorySlash Off is only required if you want to be able to access /private without a trailing slash. Ordinarily, mod_dir will append the trailing slash with a 301 redirect, since /private is a physical directory.

UPDATE: I've since realised you don't need #2 above, and you want to simply rewrite /private/<anything> to /index.php (your front-controller), even when it would otherwise map to a real file. In which case, your modified suggestion looks good:

<If "%{REQUEST_URI} =~ m#^/private#">
Order Deny,Allow
Allow from all
RewriteRule ^ /index.php [L]
</If>

It repeats the rewrite to the front-controller (which is still present later in the .htaccess file), but this is necessary to override the DirectoryIndex and to force any requests for physical files in the /private subdirectory to also be rewritten to the front-controller (so as to not be accessible).

I'm not sure what =~ m#^/private# does, but I suppose that it check if the url starts with '/private'. I'm also wonder what's the effect if the url starts with /privatesomething

Yes, it checks that the requested URL-path starts /private. =~ is the regex comparison operator and m#<regex># is the alternative syntax for regex. You can also use /<regex>/ (conventional slash delimiters) but that's naturally troublesome when checking URLs.

You can't only check that the URL starts /private/ (with a trailing slash) since a request for /private (no trailing slash) will trigger a 403 response before mod_dir appends the slash.

/privatesomething will also get caught by this but that shouldn't matter since you are only allowing access and rewriting the request - which is what you want to do anyway.

However, if you have physical files in the root that shouldn't be passed to the CMS and served directly, like /privatesomething.jpg, then this will indeed be a problem with the above regex. This can be resolved by tweaking the regex so that it matches either /private or /private/something, but not /privatesomething. For example:

<If "%{REQUEST_URI} =~ m#^/private($|/)#">

A CMS with a public folder and a private folder

Generally, when a CMS is shipped with a /public and /private subdirectory at the same level then the intention, if you have access to the server config, is to change the server's DocumentRoot to point to the /public subdirectory. OR, if you don't have access to the server config, then unconditionally rewrite the request using mod_rewrite to the /public subdirectory. And have a secondary .htaccess file in the /public subdirectory containing your front-controller pattern. (All the necessary files should be shipped with the CMS - nothing to manually edit, except perhaps to comment-out if you have access to the server config.)

This method hides the /public file-path from the visible URL (which is the intention) and naturally resolves the problem discussed above with having a URL conflict with the protected /private subdirectory.

For example, (if not changing the DocumentRoot in the server config), then in the root .htaccess file:

RewriteEngine On

# Unconditionally rewrite all requests to the "/public" subdirectory
RewriteRule ^ /public%{REQUEST_URI} [L]

Then, in /public/.htaccess you have your front-controller:

RewriteEngine On

# CMS front-controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Note the absence of the slash at the start of the RewriteRule substitution string. It should be index.php, not /index.php here.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2021-01-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。