Bobcares

Url Encoded Slashes(%2F) in Apache

by | Dec 7, 2022

Wondering how to resolve Url Encoded Slashes(%2F) in Apache? Our in-house experts are here to help you out with this article. Our server management services is here to offer a lending hand with your queries and issues.

5 Solutions to Url Encoded Slashes (%2F) Problem in Apache

Script generated url structure based on actual data coming from external sources such as user input and rss feed, may result in a “404 (Not Found)” error in Apache especially when it contains url encoded slashes.

Today, let us see the methods followed by our Support techs to resolve it:

  1. Turn on “AllowEncodedSlashes” directive in Apache

This directive may be set in server config file (e.g. httpd.conf) and may appear inside <VirtualHost> containers to affect certain websites.

Using it in .htaccess files is not allowed.

Turning on this directive tells the web server to allow encoded slashes in URLs.

2.Replace %2F with %252F and %5C with %255C after url encoding

Consider the PHP code below.

$url = ‘http://www.example.com/books/’;

$title = ‘the lamp, linux/apache/mysql/php solution’;

// replace all spaces with underscores (_) in the $title

$title = str_replace(‘ ‘, ‘_’, $title);

// urlencode the $title and replace all occurrences of encoded slashes in one line

$url .= str_replace(array(‘%2F’,’%5C’), array(‘%252F’,’%255C’), urlencode($title)) . ‘.html’;

The url will become:

http://www.example.com/books/the_lamp%2C_linux%252Fapache%252Fmysql%252Fphp_solution.html

%25 is actually the url encoded equivalent of percent (%) so the code above is actually url encoding the slashes twice.

3. Double urlencode()

Using the same variables in #2, the code below,

$url .= urlencode(urlencode($title)) . &#39.html’

will result in a url

http://www.example.com/books/the_lamp%252C_linux%252Fapache%252Fmysql%252Fphp_solution.html

Note that other url encoded characters are transformed as well. Notice the %252C. It is changed from %2C which is the encoded equivalent of comma (,).

Both #2 and #3 will work but they will make your URLs longer, ugly and harder to read.

4.Use unencoded slashes

As a good practice, we urlencode strings which may contain slashes, for use in a url. To get back the slashes, we need to replace %2F with slash (/) character.

Using same variables in #2, the following code,

$url .= str_replace(&#39%2F&#39, &#39/&#39, urlencode($title)) . &#39.html’

will output

http://www.example.com/books/the_lamp%2C_linux/apache/mysql/php_solution.html

A very nice looking slashes in the URL. However, it reads as though you have a page named “php_solution.html” inside the mysql folder. And, in fact, if you happen to have one, that page will be displayed instead.

5. Replace slashes with underscores (_)

The safest and most practical way of handling slashes, is to replace them with underscores before url encoding.

Using the variables in #2, the following PHP code (also replaces commas),

$url .= urlencode(str_replace(array(&#39/&#39,&#39,&#39), &#39_&#39, $title)) . &#39.html’

will output

http://www.example.com/books/the_lamp__linux_apache_mysql_php_solution.html

Conclusion

In conclusion, our Support Engineers showed us steps to resolve WP_Error-cURL error 7. Drop us a line in the comments if you face any trouble.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.