1

The goal is to redirect a query string, such as:

mydomain.com/sheet.csv?z=abcdef

to its respective folder, appending the query string into the folder name where the respective file is located, like so:

mydomain.com/charts/abcdef/sheet.csv

I tried the following but everything I tried results in a 404 error.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sheet.csv?z=([_0-9a-z-]+) /$1/sheet.csv [R] 

DIDN'T WORK, RESULTS IN 404 ERROR.

RewriteCond %{QUERY_STRING}     ^z=([_0-9a-z-]+)$    [NC]
RewriteRule sheet.csv chart/$1/sheet.csv [NC,L,R=301]

DIDN'T WORK, RESULTS IN 404 ERROR.

How can I fix this?


UPDATE

The error seems to be occurring because it is NOT redirecting but instead looking for the file:
mydomain.com/sheet.csv which does not and never will exist.

So why is it not redirecting, and how do I fix it?


UPDATE 2

I did a bunch of trial and error experiementing. I was able to make it do SOMETHING, just need to finish getting it working.

If I do this:

RewriteCond %{QUERY_STRING} ^z=([\w-]+) [NC]
RewriteRule ^ charts/%1/sheet.csv? [NC,L,R=301]

then it redirects to the url mydomain.com/home/mydomai/public_html/mydomain/charts/abcdef/sheet.csv

I tried every possible combination I could think of but still can't get it to just go to the URL
mydomain.com/charts/abcdef/sheet.csv

Please help thanks!

1 Answer 1

1

Yes, you cannot match QUERY_STRING in RewriteRule. Use RewriteCond instead and fix your regex pattern:

RewriteCond %{QUERY_STRING} (?:^|&)z=([^&]+) [NC]
RewriteRule ^(sheet\.csv)/?$ /chart/%1/$1? [NC,L,R=302]

Make sure this rule is placed before other rules.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.