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

Apache Redirect without include document root in rewrite rule

· 52CE 我爱测

I have this working rule: RewriteRule ^(.*)/amp/$ https://example.com/$1?amp=1 [QSA,R=301,NC,NE,L] For redirecting https://example.com/register/amp/ to https://example.com/register?amp=1 . And it is working well, but I would like…

问题原文

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

I have this working rule:

RewriteRule ^(.*)/amp/$ https://example.com/$1?amp=1 [QSA,R=301,NC,NE,L]

For redirecting https://example.com/register/amp/ to https://example.com/register?amp=1.

And it is working well, but I would like that the URL in the browser didn't change so it will load the register?amp=1, but in browser still show /register/amp/.

To achieve this behaviour, I try modifying to:

RewriteRule ^(.*)/amp/$ ./app.php/$1?amp=1 [QSA,R=301,NC,NE,L]

But then it redirects to https://example.com/home/user/public_html/register?amp=1.

So it is adding the document root wrongly and I don't know how to modify the rule to achieve that.

已采纳解答

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

RewriteRule ^(.*)/amp/$ ./app.php/$1?amp=1 [QSA,R=301,NC,NE,L]

You need to remove the R (external "redirect") flag. The ./ prefix on the substitution string is also superfluous here and just adds complexity that the OS must optimise out.

When you specify a relative path substitution in a directory context (.htaccess or <Directory> container in the server config) and you have not stated an alternative prefix (or "base" directory) with the RewriteBase directive, then Apache adds back the directory-prefix (ie. /home/user/public_html/ in this case) at the end of the rewriting process. It is because this is an external redirect that you are exposing this directory-prefix in a malformed redirect. You need an internal rewrite instead.

For example:

RewriteRule ^(.*)/amp/$ app.php/$1?amp=1 [QSA,NC,NE,L]

However, you've also included app.php in the substitution, which you've not shown in your target URLs? (app.php maybe correct - you should be rewriting to the actual filesystem path and not rely on any additional rewrites or subrequests by mod_dir.)

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

资料来源与适用范围

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

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

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