My .htaccess rewrites don't work when a file exists


#1

I am testing ready to move a static htm site to cloudways. So I set up php application>deleted index.php > replaced with index.htm, added .htaccess in the public_html and set DirectoryIndex order with index.htm first.

I added some of the .htaccess rules that work properly on my current server - A2 shared Turbo Apache hosting.

And, the rules work perfectly BUT not if I type a url to a page or file that exists on the cloudways/digital ocean servers.

These all work as they should: (i had to add spaces and such to the url’s be allowed to post)

example .co .uk > https www. example .co .uk/
www .example .co .uk >https www. example .co .uk/
https:// example .co .uk > https www. example .co .uk/
server ip address > https www. example .co .uk/
server ip address/index.php > https www. example .co .uk/
https:// www .example .co .uk/index.php > https www .example .co .uk
example .co .uk/index.php > https www .example .co .uk

BUT as soon as I type in URL or filename that exists on the server the rewrites STOP working. It’s as if I didn’t have any rewrites within the .htaccess at all.

example .co .uk/index.htm > example .co .uk/index.htm
www .example .co .uk/index.htm > www .example .co .uk/index.htm
https example .co .uk.index.htm > https example .co. uk/index.htm
https www .example .co .uk/index.htm > https example .co .uk/index.htm
server ip address/index.htm > server ip address/index.htm
www .example .co .uk/image.jpg > www .example .co .uk/image.jpg
example. co. uk/image.jpg > example .co .uk/image.jpg

But as soon as the url relates to a page or file that doesn’t exist the rewrites work fine but obviously ends in a 404 page

example .co .uk/dontexist.jpg > https www .example .co .uk/dontexist.jpg
example .co .uk/dontexist.htm > https www .example .co .uk/dontexist.htm
https:// example .co .uk/dontexist.jpg > https www .example .co .uk/dontexist.jpg

How can I get the rewrites to work when a page or file is already on the server and can be served?


#2

Try this .htaccess rule.

# Force HTTPS and www
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example\.net [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://example.net/$1 [R=301,L]

#3

Thanks, that is one of the rules I tried. I picked it up from your help on the .htaccess thread.

It does work and is the neatest solution but not once I request a file or page that exists on server it ignores my .htacess rules.

Technical support have answered my ticket query and apparently it’s to do with my 100% static site - I don’t use wordpress or anything like that, its very old fashioned and hand coded locally and then uploaded! So my understanding is the nginx part is serving the static resources immediately if they exist. This is great if your site creates the static resources itself on the apache side. But, despite being super fast, if the static resource exists it doesnt think it needs to go through apache and therefore not checking the .htaccess file and therefore not following the rewrites. So a unique problem to the way I do my site without SQL or php and so on. Can you tell I don’t really know what I’m talking about!!!

Support are helping and I have to say, so far have been excellent, giving me a quick and detailed answer and solution.


#4

Tech support have been fantastic and sorted it for me.

If anyone else has a 100% html5 static site that they upload to the public_html folder. ie no wordpress, sql, cms, php etc. Then I recommend you contact support if you know your rewrite coding rules are correct but find they aren’t used when accessing content that exists on the server.

The move to cloudways has so far been positive with faster page loads and server response in Europe, US and Asia in comparison to my old shared hosting. I’m very pleased.