Sunday, October 30, 2016

mod_rewrite testing with XAMPP should be done carefully. RewriteCond %{REQUEST_FILENAME} !-f can cause some confusion

Web Server: XAMPP (both applies to other packages as well)

Problem: Strange , unexpected results from simple .htaccess mod_rewrite operations. RewriteRule doesn't seem to work.


When you would like to test your apache's .htaccess URL rewrite, make sure you test with a proper URL.

For example, if you have a RewriteRule like this:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ".*/$1" "/public/index.php?p=pageabc&t=$1" [NC,L,R=301]

Make sure you're testing your site with a URL like this:
http://yoursite.com/test-term

and not with something like this:

http://yoursite.com/public/index.php?***********************

The reason is obvious. The first RewriteCond won't be true and the rule won't apply.

Also, please note that by default, the url directory to your .htaccess file is automatically removed from the request's URL. So you can fetch only the part after yourdomain.com/ or yourdomain.com/subdir, if your .htaccess is inside of subdir folder.

Also, if you don't want to redirect user to target URL (2nd parameter of RewriteRule), you should remove R=301 from the RewriteRule.

Caching of URL rewrites


You should be aware of the fact that your browser might save some URL redirects and serve them any time you surf the website in future. This means that you should clear your browser cache before testing URL rewrites.

Your .htaccess doesn't get called?


A good trick to see if your .htaccess is actually getting fired at all, is to try to input a syntax error into it. That should crash the site if .htaccess is processed.

No comments:

Post a Comment

Ubuntu 12.04, 14.04, 16.04 - auto start an app or script before login

To run a command or application at startup, even before the user has logged in, you can use this file: /etc/rc.local The commands entered...