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

ErrorDocument in dynamic SubFolder

· 52CE 我爱测

I host different Websites in 1 web space. But I don't want to give the ErrorDocument an absolute SubFolder name. Is it possible to make website a variable? ErrorDocument 404 /website/error?error=404 Can .htaccess auto detect the …

问题原文

原帖链接 · julianstark999 · CC BY-SA 3.0

I host different Websites in 1 web space. But I don't want to give the ErrorDocument an absolute SubFolder name.

Is it possible to make website a variable?

ErrorDocument 404 /website/error?error=404

Can .htaccess auto detect the SubFolder (including SubFolder depth)?

For example, a website written by myself where I've the same "system" twice or more e.g. productive, test and development. So all of them have different SubFolders. If someone wants to host the website by himself and use the SubFolder root/website/test/MyWebsite. So .htaccess needs to auto detect the SubFolder to redirect to the error page. So the user doesn't need to change the absolute path to website/test/MyWebsite/error....

Server version: Apache/2.4.12 (Win32)
Apache Lounge VC11 Server built: Mar 20 2015

EDIT: This forks fine for myself

RewriteCond %{REQUEST_URI} ^/([^\.]+)/
RewriteRule ^ - [E=SUBDIR:%1]

ErrorDocument 404 /%{reqenv:SUBDIR}/error?error=404

已采纳解答

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

Server version: Apache/2.4.12 (Win32)


Is it possible to make website a variable?

Not before Apache 2.4.13 unfortunately.

Can .htaccess auto detect the SubFolder (including SubFolder depth)?

This depends on the module you are using in .htaccess, rather than .htaccess itself. For instance, mod_rewrite (when used in a per-directory context) works relative to the directory in which it is contained and can be used to extract the subdirectory path from the URL. ErrorDocument is a core directive, paths are relative to the DocumentRoot.

If you were on Apache 2.4.13+ then... from the Apache docs for ErrorDocument:

From 2.4.13, expression syntax can be used inside the directive to produce dynamic strings and URLs.

Which means you could do "something like" this:

# Assign the subdirectory to an environment variable
# Note the slash prefix is excluded from the captured pattern
RewriteCond %{REQUEST_URI} ^/([^/]+)/
RewriteRule ^ - [E=SUBDIR:%1]

# Set ErrorDocument dynamically to this subdirectory
# Requires Apache 2.4.13+
# The 2nd argument to the ErrorDocument directive must be prefixed with a slash
# in the source code itself for it to be interpreted as a local path.
ErrorDocument 404 /%{reqenv:SUBDIR}/e404.php

Alternative - Subdomains

An alternative, that would work on all versions of Apache, is to use subdomains instead of subdirectories. The subdomain can point to the subdirectory if you wish, but you would need to access the site through the subdomain.

The important thing with the subdomain is that the document root changes. So your ErrorDocument directive can be kept simple:

ErrorDocument 404 /error?error=404

The path (starting with a slash) is relative to the DocumentRoot.

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

资料来源与适用范围

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

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

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