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
RewriteRule
that prevents an undesirable "loop"?php/inwork.php?refpage=/php/inwork.php
- In other words, it rewrites to itself andrefpage
ends up containing the script that is processing the request, instead of the URL being requested. If that isn't happening then there must be some other interaction we are not seeing... other directives, other.htaccess
files (perhaps in the/php
subdirectory)?