大网数据广告活动
服务器运维问答

Htaccess rewrite to file located up one level

· 52CE 我爱测

I have an htaccess in a subdomain with the following rule: RewriteEngine On RewriteBase / RewriteRule ^feed([0-9]+)$ ../image.php?id=$1 [L] So basically I want to redirect requests from feed which is located in a subdomain to ima…

问题原文

原帖链接 · Michael Samuel · CC BY-SA 3.0

I have an htaccess in a subdomain with the following rule:

RewriteEngine On
RewriteBase /
RewriteRule ^feed([0-9]+)$ ../image.php?id=$1 [L]

So basically I want to redirect requests from feed which is located in a subdomain to image.php file which is located in the main domain (just one directory above). Using ../ doesn't seem to work like in PHP.

So how can I rewrite to file in the parent directory?

已采纳解答

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

By the sounds of it, you are already in the document root of your subdomain, which is probably defined in its own virtual host (which happens to point to a subdirectory off the main domains document root).

Unfortunately, you can't internally rewrite above/outside the document root using mod_rewrite in .htaccess. In .htaccess the RewriteRule substitution takes a URL-path only, not a filesystem path. However, you can do this if you are using mod_rewrite directly in the server config. In the server config you can specify a filesystem path as the substitution (you would need to explicitly include the full path, you can't use a relative path like ../image.php).

Alternatively, in .htaccess you might be able to proxy the request from your subdomain to the maindomain using mod_proxy with mod_rewrite. However, this might still require some server configuration. For example:

RewriteRule ^feed([0-9]+)$ http://maindomain.com/image.php?id=$1 [P]

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

资料来源与适用范围

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

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

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