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

Decoding a Rewritten URL using mod_rewrite - htaccess - regex

· 52CE 我爱测

I am using Apache 2.4. I have set up and configured an Apache HTTP server, to host my invision app, my app is linked to external google docs. I am trying to redirect to an external google doc from the app but Apache does not like…

问题原文

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

I am using Apache 2.4. I have set up and configured an Apache HTTP server, to host my invision app, my app is linked to external google docs. I am trying to redirect to an external google doc from the app but Apache does not like it.

This is the URL it tries to access

http://example.com/redirect? 
url=https%3A%2F%2Fdocs.google.com%2Fforms%2Fd%2Fe%2F1234567890%2Fviewform

I have Rewritten the URL using the following:

RewriteCond %{QUERY_STRING} ^(.+)(forms.+)
RewriteRule ^redirect(.*) https://docs.google.com/%2 [R=302,QSD]

and after the rewrite, I am left with this.

https://docs.google.com/forms%2Fd%2Fe%2F12345678900%Fviewform

This does what it is supposed to and if I personally replace the %2F with '/' it brings me to the document so I know it is close. The URL redirects to google drive, only I get a

404 page not found

message from google drive. I know I need to decode the URL in order to successfully reach the document but how can I do this using .htaccess or any other means?

AllowEncodedSlahes on (I have also tried NoDecode) //VirtualHost conf and apache conf

I have tried matching and replacing the %2F's using Regex but they are not recognized and I get 500 - Internal Server Error.

I understand that this seems to be an ongoing bug/issue but does anyone at all have a solution to this incredibly painful problem please? I am at my wits end with this, Thank you.

已采纳解答

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

and after the rewrite, I am left with this.

https://docs.google.com/forms%2Fd%2Fe%2F12345678900%Fviewform

Although that wouldn't happen with the directives you posted, unless you also included the NE (noescape) flag on the RewriteRule directive. (Instead, it would be doubly encoded). Using the NE flag is a step in the right direction, however, we need another step to URL decode the resulting URL-path...

Side note: The resulting URL-path you posted above has a typo (given your example input URL). The final %2F is missing; instead, you have 0%F. It should read: forms%2Fd%2Fe%2F1234567890%2Fviewform

Google would seem to be triggering the 404 because of the encoded slashes in the URL-path (as you suggest), just as Apache will do by default. Setting AllowEncodedSlashes On (in the vHost) will not immediately help with this as the 404 is being triggered by Google. However, this does need to be set so we can process the URL further and unencode the encoded slashes, before issuing the final redirect.

Try the following instead:

# Set in your vhost (server config)
AllowEncodedSlashes On

Then, in .htaccess (or appropriate <Directory> section):

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.+)(forms.+)
RewriteRule ^redirect$ /redirect/%2 [NE,QSD]
RewriteRule ^redirect/(.+) https://docs.google.com/$1 [R=302,L]

So, given an initial request to your server of:

/redirect?url=https%3A%2F%2Fdocs.google.com%2Fforms%2Fd%2Fe%2F1234567890%2Fviewform
  1. The first RewriteRule internally rewrites the request to (still on your server):

    /redirect/forms%2Fd%2Fe%2F1234567890%2Fviewform
    

    This is "allowed" (and does not trigger a 404) because of the AllowEncodedSlashes On directive you have set in the server config.

  2. Then, the second RewriteRule decodes the URL-path (%2F to /) and issues an external redirect to:

    https://docs.google.com/forms/d/e/1234567890/viewform
    

    Apache automatically URL decodes the URL-path before the RewriteRule pattern is applied. The relevant part is captured and passed through to the substitution in the $1 backreference.

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

资料来源与适用范围

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

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

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