I have to redirect a domain to a subfolder using .htaccess. I have done this with
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de$ [NC]
RewriteRule !^domain\.de/ /domain.de%{REQUEST_URI} [L,NC]
This works for 90% of my site.
But I also have file reference like:
domain.de/templates/shaper_helix3/fonts/fontawesome-webfont.woff2?v=4.3.0.html
The file exists in the filesystem with the filename:
fontawesome-webfont.woff2?v=4.3.0.html (including query string)
These files are currently not found (404).
If I manually remove the query string from the filename (e.g. resulting in fontawesome-webfont.woff2) I get a HTTP Code 200 (Found).
I have multiple files with said filename structure and can not "hardcode" them, because through updates and installation of additional modules via CMS I can not know, which files will exist in the future in the respective directories.
The reason for the files inlcuding a Query String / version in their names is cache busting.
How do I have to modify my rules, so that these files can also be found?
CodePudding user response:
With your shown samples/attempts, please try following htaccess rules. These rules are for internal rewrite ones.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^(templates/shaper_helix3/fonts/fontawesome-webfont\.woff2)?(v=4\.3\.0\.html)/?$ $1?$2 [QSA,NC,L]
OR if it could be any file under templates/shaper_helix3/fonts/fontawesomefont/ then try following:
RewriteEngine ON
RewriteRule ^(templates/shaper_helix3/fonts/[^%]*)?(v=4\.3\.0\.html)/?$ $1?$2 [QSA,NC,L]
CodePudding user response:
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de$ [NC] RewriteRule !^domain\.de/ /domain.de%{REQUEST_URI} [L,NC]
The "problem" here is that the REQUEST_URI server variable is already %-decoded so ends up rewriting a request for /fontawesome-webfont.woff2?v=4.3.0.html to /domain.de/fontawesome-webfont.woff2?Fv=4.3.0.html (ie. a file fontawesome-webfont.woff2 with a query string of v=4.3.0.html, not a literal file called fontawesome-webfont.woff2?v=4.3.0.html). You need to maintain the %-encoding through the rewrite so ? is rewritten to ?, ie. /domain.de/fontawesome-webfont.woff2?v=4.3.0.html (which is later decoded when the request is finally mapped to the filesystem), not ? (that delimits an actual query string).
You can do this by capturing the URL-path with a backreference (also %-decoded) but use the B flag that re-encodes the backreference before rewriting the request.
For example:
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de$ [NC]
RewriteCond %{REQUEST_URI} !^/domain\.de/ [NC]
RewriteRule (.*) domain.de/$1 [B,L]
