I'm trying to change the DocumentRoot to subfolder
through .htaccess.
This works well...
RewriteCond %{REQUEST_URI} !subfolder/
RewriteRule ^(.*)$ /subfolder/$1
...however, I only want to do this when a specific query parameter (var=test
) is set, so I have this:
RewriteCond %{QUERY_STRING} ^var=test$
RewriteCond %{REQUEST_URI} !subfolder/
RewriteRule ^(.*)$ /subfolder/$1
...but for some reason, this doesn't work.
Any suggestions as to what the problem could be?
UPDATE: Here is a full (simplified) example:
.htaccess
RewriteEngine on
RewriteCond %{QUERY_STRING} (.*(?:^|&))var=test((?:&|$).*)
RewriteCond %{REQUEST_URI} !sites/test/
RewriteRule ^(.*)$ /sites/test/$1
sites/test/index.php
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<p>Test</p>
</body>
</html>
sites/test/styles.css
* {
color: red;
}
Now try loading the page with http://example.com/?var=test
. The Test
text should be red, but it's not.
Remove the QUERY_STRING RewriteCond
line, and it works.
But I need to be able to check for the query string somehow..
Apache Virtual Host (in case it's needed)
<VirtualHost *:80>
ServerName www.example.com
ServerAdmin admin@example.com
DocumentRoot "/var/www/mainsite"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/mainsite">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
var=test
the only parameter? A better rule would beRewriteCond %{QUERY_STRING} (.*(?:^|&))var=test((?:&|$).*)
if it isn't. See: wiki.apache.org/httpd/RewriteQueryString – Stephen Ostermiller♦ Jan 10 '18 at 21:22RewriteCond %{QUERY_STRING} (?:^|&)var=test(?:&|$)
– MrWhite Jan 10 '18 at 22:26