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

.htaccess redirect with all parameters

· 52CE 我爱测

I have a simple rule in the .htaccess file, to redirect all requests to inwork.php : RewriteRule .* php/inwork.php?refpage=%{REQUEST_URI} [L] But all parameters are missing. For the request http://example.com/example.php?check=1&…

问题原文

原帖链接 · Rolf Hacker · CC BY-SA 4.0

I have a simple rule in the .htaccess file, to redirect all requests to inwork.php:

RewriteRule .* php/inwork.php?refpage=%{REQUEST_URI} [L]

But all parameters are missing. For the request http://example.com/example.php?check=1&check=2, I get example.php in inwork.php for $_GET['refpage']. But I want example.php?check=1&check=2 in inwork.php. (There are requests with many parameters in different orders, so I don't want to write them manually in .htaccess.)

Edit: Full htaccess:

Options -Indexes
RewriteEngine On
RewriteBase /

AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml         text/css application/x-javascript application/javascript

RewriteCond %{HTTP_HOST} ^domain.de [NC] 
RewriteRule ^(.*)$ https://www.domain.de/$1 [L,R=301,NE]

RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{THE_REQUEST} index.html [NC]
RewriteRule ^(.*)index.html /$1 [NC,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^(.*)/pics/(.*)$ [NC]
RewriteRule .* php/inwork.php?refpage=%{REQUEST_URI} [B,L]

已采纳解答

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

tl;dr Alternatively, just rewrite to php/inwork.php and parse the PHP superglobal $_SERVER['REQUEST_URI'] instead, which already contains the URL-path and query string from the initial request. (Note that the $_SERVER['REQUEST_URI'] PHP variable is different to the REQUEST_URI Apache server variable, despite the name being the same.)


The REQUEST_URI Apache server variable contains the URL-path only, not the query string. If you want to include the query string from the original URL and make this part of the refpage URL parameter then you need to append the query string as an additional step.

However, I think it is "easier" to grab the entire URL (URL-path + query string) from the THE_REQUEST Apache server variable and pass this instead:

For example:

RewriteCond %{THE_REQUEST} \s(/.+)\s
RewriteRule ^ php/inwork.php?refpage=%1 [B,L]

The %1 backreference contains the full URL (URL-path + query string) from THE_REQUEST, which contains the initial line from the HTTP request (eg. GET /example.php?check=1&check=2 HTTP/1.1).

The B RewriteRule flag URL encodes the captured backreference - necessary in order to make sure everything ultimately belongs to the refpage URL parameter and not become part of the query string on the rewritten request.

You'll then need to manually parse the complete URL (value of the refpage URL param) into its component parts with PHP (which I assume you are already doing)...

For example... given a request for /example.php?check=1&check=2 then the PHP variable $_GET['refpage'] now contains /example.php?check=1&check=2 (having been automatically URL decoded by PHP). (Possibly just your hasty example, but the two check URL params will need special treatment if you want to avoid the later overwriting the former.)


However, unless you have a specific requirement to do it this way, I would simply parse the PHP superglobal $_SERVER['REQUEST_URI'] instead (as mentioned at the top of my answer). This already contains the complete URL from the request - the same value you are manually assigning to the refpage URL parameter. You can then simplify your rewrite:

RewriteRule .* php/inwork.php [L]

UPDATE:

RewriteCond %{THE_REQUEST} index.html [NC]
RewriteRule ^(.*)index.html /$1 [NC,R=301]

You are missing the L flag on the RewriteRule directive. This is a potential problem, since if this directive is triggered (removing index.html) processing will continue and will likely be externally redirected to /php/inwork.php. However, this shouldn't have affected your example URLs.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^(.*)/pics/(.*)$ [NC]
RewriteRule .* php/inwork.php?refpage=%{REQUEST_URI} [B,L]

The first condition here (checking that the request does not map to file) is what prevents the undesirable second loop back to itself.

The second condition that checks that /pics/ is not present in the URL-path can be simplified from !^(.*)/pics/(.*)$ to !/pics/. (No need for capturing subpatterns if they are not being used.)

The B flag is not required here. However, by using the REQUEST_URI server variable, you will naturally lose the query string parameters.

What you can try, is using the QSA flag on the RewriteRule. For example:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/pics/ [NC]
RewriteRule .* php/inwork.php?refpage=%{REQUEST_URI} [QSA,L]

Although this won't do specifically what you are asking, as the query string parameters won't be part of the refpage URL param, they will be merged with the URL you are rewriting to and consequently should be available in PHP's $_GET array. For example, given a request for /example.php?check=1&check=2 then the $_GET array would be populated as follows:

Array (
  'refpage' => '/example.php',
  'check' => '2',
)

(Because the check URL param is duplicated.)

Additionally, you should also make sure that MultiViews is disabled. Although whether this is actually causing problems in this instance is dependent on your file structure. (It shouldn't be an issue with the example URL you posted.)

Options -Indexes -MultiViews

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

资料来源与适用范围

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

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

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