Search code examples
php.htaccessmod-rewritexampphtdocs

How do I use the mod_rewrite RewriteRule with an "R" flag to rewrite the url to one on localhost?


I'm getting started on learning to use mod_rewrite. Here's my test .htaccess file under xampp/htdocs:

RewriteEngine On
RewriteRule ^something/?$ test.php [R,NC,L]

The rewrite works without the R flag, but when I add that in and access localhost/something, the url it redirects to is http://localhost/C:/xampp/htdocs/test.php. When I searched it, I read that R should only be used for external redirects, and to confirm this, I replaced test.php with http://google.com and it worked.

Is there a reason I shouldn't be trying to do this? If not, then how do I make it rewrite to localhost/test.php?


Solution

  • You need to provide the right path, use /test.php instead and define your RewriteBase. You should also provide a redirect status, either R=301 or R=302 in your RewriteRule flags.