"Rule caching" in .htaccess - How to force apache to re-read a modified .htaccess?

6,937

I am using XAMPP on Windows 7, and was experiencing the same issue. From my experimentation it appears that the caching of redirects is being carried out by the web browser, not Apache.

To demonstrate:

  • Add the following rule to your .htaccess file: RewriteRule ^fail/?$ fail.php [L,R=301]
  • Visit /fail in your web browser. You should be redirected to fail.php
  • Change the reference in your .htaccess file from fail.php to fail2.php
  • Visit /fail in your web browser again.
  • You should be redirected to fail2.php, but you end up at fail.php
  • Now visit /fail in a different web browser on the same machine, you should end up correctly at fail2.php.

The browser that was giving me issues was Firefox 6.0.

It appears that newer web browser will cache HTTP 301 redirects locally - they are defined as a permanent redirect, so should never change. However it's a pain when you are a developer and still working on a site and changing things.

Share:
6,937

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I am reorganizing the link structure of a dynamic php-based site of mine. What I am trying to obtain is:

    • redirect old links onto new ones;
    • create new RewriteRules for new links, since every 'link' to an html page is mapped onto a php page with parameters.

    The only .htaccess file is contained in the website root folder. There are no other .htaccess files in subfolders.

    Till today I had no problems in reorganizing links by modifying the rules in .htaccess. Since yesterday I have been experiencing a strange problem: it looks like modifications to the .htaccess file are not immediately read by the Apache web-server, that keeps applying the old rules even if they are no longer contained in the .htaccess file. For example, I have a rule that performs the following mapping:

    /cat-a/subcat-b/page.html -> /file.php?cat=cat-a&subcat=subcat-b
    

    If I modify that rule as follows:

    /cat-a/subcat-b/page.html -> /file.php?XXcatXX=cat-a&subcat=subcat-b
    

    Apache keeps rewriting URLs according to the old (cat=cat...) format for a while. The new format (XXcatXX=...) is used only after a random lapse of time. Random meaning that I could not find any pattern in the way it behaves.

    Furthermore, if I delete a rule, Apache keeps using the deleted rule for a while.

    The only regularity I have found concerns new rewrite rules: Apache reads them immediately and starts using them. If I delete a rule or modify an existing one, Apache ignores the changes for a while.

    I am noticing this problem only on RewriteRule instructions and not on redirections. Since redirections and rewritings are managed by different modules, I assume that I have to modify some settings in the mod_rewrite configuration.

    I made some searches but was not able to find any information/documentation on this behaviour. May be there is something that I ignore and I am missing.

    Are there some settings I have to worry about? Have you ever experienced a similar problem?

    I am using XAMPP on a Mac OS SnowLeopard machine.

    Thanks. Regards. A.

  • MrWhite
    MrWhite about 4 years
    FWIW, the OP appears to be discussing an internal rewrite, not an external 301 redirect. However, browser cache is still the most probable cause. Note that the caching of 301 redirects can be controlled by sending the appropriate Cache-Control (and Expires) HTTP headers in the redirect response.