Azure Web App Doesn’t Read HTAccess File

If you are a .NET developer, chances are you’ve used Azure before. It’s almost the defacto choice these days for keeping your stack “Microsoft”. But Azure also has the ability to host a whole range of platforms and frameworks, including common web stacks like PHP/LAMP.

Recently I’ve been spinning up cheap Azure Web Apps for Linux to host various PHP applications. Costs start around the $10 mark which is very good pricing when you compare it to any other shared hosting or linux VPS options out there.

But I quickly ran into issues where my application’s .htaccess file was not being respected when deployed to Azure. Notably, any rewrite rules or redirects were not being honoured. As it turns out, there’s really two particular issues that it always seems to boil down to.

You Are Using A Windows App Service

If you are using a Windows App service, even with a PHP stack, it will utilize IIS instead of Apache. Remember that .htaccess is specific to Apache only. To avoid other similar issues, I would highly recommend switching your App Service to Linux. It can be daunting if you’ve never used a linux machine before, but for the most part, all of the operating system specifics are abstracted away and the App Service functions much like it would if it was Windows.

If you have a requirement to use a Windows App Service, then you must take all of your .htaccess rules and translate them to a web.config (Which is what IIS uses). e.g. If you have a rewrite rule in your htaccess, then you must add that rule to a web.config file and upload it to the root of your application.

Again, I’ll re-iterate, you cannot use htaccess files on a Windows App Service.

You Are Using PHP 8 On A Linux App Service

Now comes a “feature” that I could find absolutely zero documentation for! I came to find that if you used PHP 8 instead of 7 as your App Service Stack, you are actually using nginx as a web server instead of Apache!

You can check this on your App Service by going to Configuration -> General Settings.

Again, if you have PHP 8 selected here, your application will run under nginx which does not read htaccess rules. Ugh!

The solution for this is maybe a little bit more nuanced than that of the Windows App Service. You *can* just downgrade the Major PHP version without much fuss and your application will likely function just the same. However if you were utilizing PHP 8 features, or you want the latest and greatest, then you will need to translate your htaccess rules to nginx configuration.

Again, I could find absolutely no documentation on this, so I eventually just downgraded the PHP version to 7 and lived with it. For me, most of my applications are lightweight proof of concepts so running on the latest and greatest isn’t that big of a deal, but your mileage may vary!

Leave a Reply

Your email address will not be published.