Question

How to use mod_rewrite in .htaccess on CloudPanel domain

Posted September 5, 2020 116 views
LAMP StackDigitalOceanInitial Server SetupDebian 10

I’m currently migrating a CodeIgniter website to a CloudPanel droplet. On that website, I have added two rewrite rules in the standard CI .htaccess file in the public folder:

RewriteRule ^css/(.*)\.[0-9]+\.css css/$1.css [L]
RewriteRule ^js/(.*)\.[0-9]+\.js js/$1.js [L]

The purpose of this is to give the css and js assets a timestamp to force browsers to updated cached assets if there are new versions.

This works well both on the old server and on my localhost server, but on the CloudPanel domain the files are not found.

If I manually write the address in the browser and remove the timestamp, I find the files.

I’ve seen from other threads here that I might have to use some console commands to change the apache settings to allow rewriting urls. But I am wondering if there is a setting in CloudPanel that I should be using.

Is there a setting in CloudPanel I need to change to allow rewrite rules in .htaccess?

1 comment
  • Hypertext Access File, or most known as .htaccess, is a configuration file for Apache web servers that can be used to define very specific configuration options. Configurations can become quite granular with the use of regex however, most users typically stick to using popular .htaccess examples such as redirecting web pages or setting custom headers.

    Although .htaccess can be quite useful, it can also be somewhat of a challenge to figure out where the issue lies given you are faced with an .htaccess not working. This post provides a few tips for helping to resolve this issue by identifying a few common .htaccess problems that you can check in your own .htaccess file, as well as a few troubleshooting techniques.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi there @thomasstorfjord,

The .htaccess file works only with Apache webserver, however CloudPanel comes with Nginx rather than Apache.

You would need to convert your .htaccess rule to Nginx rule and add the new rule to your Nginx server block.

For more information you could take a look at this article here:

https://www.nginx.com/blog/converting-apache-to-nginx-rewrite-rules/

Hope that this helps.
Regards,
Bobby

  • Thank you so much, Bobby! That would have taken me weeks to figure out.

    If anyone from the future is wondering, here is my converted code:

    location ^~ /css/ {
      rewrite ^/css/(.*)(\.[0-9]+).css /css/$1.css break;
    }
    
    location ^~ /js/ {
      rewrite ^/js/(.*)(\.[0-9]+).js /js/$1.js break;
    }
    
Submit an Answer