.htaccess URL rewrite

I have a Wordpress site using the Nextgen gallery. the images being uploaded by the client are names with, unfortunately, spaces. All was fine initially as Wordpress replaced the space with "%20" and was able to display the images.

Now, for some reason, %20 is being replaced with a +. So any image that exists in the gallery with a name like "image name.jpg" the URL is now being written as image+name.jpg and its throwing a 404.

I would like to use .htaccess to rewrite all + signs as %20 so all images existing in the galleies can be displayed.

Doable? Thanks in advance.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Use this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule "^([^+]*)\++([^+]*\+.*)$" $1\ $2 [N]
RewriteRule "^([^+]*)\++([^+]*)$" $1\ $2 [N]

Open in new window

Steve BinkCommented:
I'm not sure mod_rewrite is the tool to use for this job.  While it can use regex for pattern matching and basic replacement, it does not really offer a full regex engine.  For example, skij's solution will likely only work for filenames with a maximum of two converted spaces.

A better idea is to intercept the request with mod_rewrite and send it a custom script designed to find the appropriate file to display.  For example:
RewriteCond (%{REQUEST_FILENAME}) !-f
RewriteRule .* my-special-handler.php?fn=%1 [NC,L]

Open in new window

Inside my-special-handler.php, you can use standard string manipulation to parse the filename and execute a best-guess-match to figure out which one to send.
Steve Bink, I respectfully disagree with you.  The regex engine used by mod_rewrite is quite powerful.  The sample I provided will work with ANY number of plus symbols and/or spaces, even more than two.  For example:
will point to:
a b c d e f g.png

Using a special PHP handler is unnecessary, would diminish performance and could result in security vulnerabilities.   In this case, a pure mod_rewrite solution is best.
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Steve BinkCommented:
My apologies, I read the rules poorly by not paying attention to the [N] flag.  You are correct - that rule will pick all the encoded spaces.  

By "not a full regex engine" I mean that mod_rewrite is wonderful for pattern matching, but not so much at complicated replacing complicated patterns.  RewriteMap is good for that, which leads back to the PHP handler..  :)

For simple use cases like this (only one pattern), I agree with you - mod_rewrite is more than sufficient.  For multiple patterns, I would use a handler.  The security concerns are not high for a simple download script, and management would be easier.
mcpilot1Author Commented:
I found my own solution- because of the fact that the image thumbnails were automatically being assigned +'s to fill in the blanks, the rewrite was actually not working because the images werent being clicked yet.

I had to manually change the names of the files. Fortunately, I was able to do that in batch.

Thank you for your assistance,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mcpilot1Author Commented:
the solution was actually completely outside the mod-rewrite aspect of this question itself.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.