Reputation: 9890
I've looked around a lot, but can't find anything that really explains much. I've used htaccess rewrites before without much issue, but I can't seem to find where this one's failing. I think it has something to do with the initial (original) URL, but I can't seem to find a working combination.
HTML:
<link rel="stylesheet" type="text/css" href="http://localhost/site/vnd/style/main.css?p=desktop">
.htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^style/([A-Z]+\.css)\?p=(.*)$ style/$2/$1 [R=301,L]
Expected redirect (where the file actually lives):
style/desktop/main.css
In regex testers it splits and re-creates it just fine, but I can't see what Apache sees, so it's hard to debug. I've tried combinations of absolute and relative paths, trailing and no-trailing slashes, etc.
In the console, a 404 is being output, telling me that the redirect isn't working at all, and is still looking for the original URL. Headers:
Request URL: http://localhost/Patterns/mvc/style/main.css?p=desktop
Request Method: GET
Status Code: 404 Not Found
Upvotes: 0
Views: 317
Reputation: 9007
RewriteRule
s are not capable of picking up query strings. You'll need to use a condition to capture it, and pass it on accordingly:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^p=(.*)
RewriteRule ^style/([a-z]+)\.css$ style/%1/$1.css [R=301,L,NC]
Notice that I have moved the p
key to the QUERY_STRING
condition, and that I have added the NC
flag to the rule (which means that it will ignore the case of the string).
Upvotes: 1