Reputation: 335
I'm moving an old site into an /archive sub directory of a the new website. The site is all HTML has lots of fully qualified URL to when it wasn't in the archive folder (e.g. (http://domain.com/pictures/home.html
). This is fine as I have rewritten any unfound files to the /archive directory.
The problem is... a lot of these fully qualified URLs have # in the filename e.g. http://domain.com/pictures/category#001.html
. These are then linked to as http://domain.com/pictures/category%23001.html
. When the rewrite is applied to these URLs, the server doesn't find the file giving a 404 error, with the path truncated at the %23 (e.g. '/archive/pictures/category was not found on this server.')
I've tried using the [B] flag, which still gives the 404 error but the file path it gives does exist (e.g '/archive/pictures/category#001.html was not found on this server.').
If I navigate directly to the file in the archive directory using the %23
in the URL(e.g. http://domain.com/archive/pictures/category%23001.html
) then the everything works fine. The only issue is when adding the /archive directory through the RewriteRule
The rule is simply this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /archive/$1 [B,L,QSA]
I've also tried replacing the rule with this:
RewriteRule (.*) /archive/%{REQUEST_URI} [B,L,QSA]
But it has the same issue.
Thanks for reading this far!
Upvotes: 0
Views: 330
Reputation: 335
In the end the only way I could get the server to go to the file was to redirect to it, instead of rewriting the URL, using the following code (R=302 flag added).
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /archive/$1 [R=302,B,L,QSA]
Upvotes: 0
Reputation: 785196
You need to use NE
flag here (which avoids escaping):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ((?!archive/).*) /archive/$1 [NE,L,NC]
Upvotes: 1