I'm using this rewrite rule in my .htaccess to create a SEO friendly URL
The .htaccess rule cannot "create" anything. URLs are created in the links you publish on the pages of your site. Only once that link is clicked is anything sent to the server.
The mod_rewrite rules deal with that incoming request. They either respond with a redirect to a different URL, or internally rewrite the request to get the content from an internal non-default location inside the server, or in the case of not matching the pattern mod_rewrite does nothing at all for the current request.
RewriteRule ^tools$ http://www.example.com/focus/tools.php [L]
When user asks for
example.com/tools
the above rule creates an external 302 redirect to the new URL
http://www.example.com/focus/tools.php
.
What you actually needed was an internal rewrite:
RewriteRule ^tools$ /focus/tools.php [L]
RewriteRule ^focus/tools\.php$ http://www.example.com/tools [R=301,L]
The code for the redirect is correct. However, when both rules are used on the site, you will get an infinite redirect loop.
The solution is to let the redirect happen only for direct client requests and not when the filepath has already been rewritten.
# External redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /focus/tools\.php\ HTTP/
RewriteRule ^focus/tools\.php$ http://www.example.com/tools [R=301,L]
#
# non-www to www canonicalisation redirect
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Internal Rewrite
RewriteRule ^tools$ /focus/tools.php [L]