1

I have the following .htaccess file:

RewriteEngine On

# Rewrite www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Suppose my domain is foo.com. If I open http://foo.com/something, it will be redirected to http://www.foo.com/something. This works nicely, except when something contains any url encoded characters.

If I open http://www.foo.com/bar/file-with-%3F-in-name, the REQUEST_URI is /bar/file-with-%3F-in-name.

If I open http://foo.com/bar/file-with-%3F-in-name, however, the url encoded %3F is decoded to ? during rewrite, and the REQUEST_URI becomes /bar/file-with- with the QUERY_STRING -in-name.

How can I keep the url encoded characters from being decoded during rewrite?

I've tried using the B and NE flags, but without luck.

1 Answer 1

1

Here is version of rule that will solve your problem:

# Rewrite www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{THE_REQUEST} \s/+(\S*)\sHTTP
RewriteRule ^ http://www.%{HTTP_HOST}/%1 [L,R=301,NE]
  • Trick is to grab the request URI from THE_REQUEST variable before it gets decoded by Apache.
  • NE is still needed to avoid %3F becoming %253F

It was my mistake earlier that I didn't interpret question correctly.

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.