Chris
Chris

Reputation: 282

Mod_rewrite not working for some rules unless first letter capitalized? HTTPS Server

I've been working on this site for quite awhile. Im employing a .htaccess file to get clean URLS. This worked fine on my personal test server, which is http, and on my localhost, but after moving to the production server (with HTTPS enabled and working), some of the rules aren't working

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^index/?$  index.php [NC,L] 
  RewriteRule ^home/?$  index.php [NC,L] 
  RewriteRule ^main/?$  index.php [NC,L] 
  RewriteRule ^about/?$  about.php [NC,L] 
  RewriteRule ^music/?$  music.php [NC,L] 
  RewriteRule ^shows/?$  shows.php [NC,L] 
  RewriteRule ^blog/?$  blog.php?page=1 [NC,L] 
  RewriteRule ^blog/([\0-9]+)/?$   blog.php?page=$1   [NC,L]
  RewriteRule ^contact/?$  contact.php [NC,L] 
  RewriteRule ^profile/?$  profile.php [NC,L] 
  RewriteRule ^manage-site/?$  manage-site.php [NC,L] 
  RewriteRule ^my-blog-posts/?$  my-blog-posts.php [NC,L] 
  RewriteRule ^new-blog-post/?$    new-blog-post.php   [NC,L]
  RewriteRule ^edit-music/?$  edit-music.php [NC,L] 
  RewriteRule ^admin-login/?$    admin-login.php   [NC,L]  
  RewriteRule ^admin/?$    admin-login.php   [NC,L]  
  RewriteRule ^forgot-password/?$   forgot-password.php [NC,L]
  RewriteRule ^password-reset/?$    password-reset.php [NC,L]


  RewriteRule ^blog/article/([\w-]+)/?$   blog-post.php?slug=$1   [NC,L]  #handle requests for Individual Blog Posts
  RewriteRule ^blog/article/([\w-]+)/([^/\.]+)/?$   blog-post.php?slug=$1&reply-comment=$2   [NC,L]  #handle requests for Individual Blog Posts with a comment specified for replies when JavaScript is disabled
  RewriteRule ^password-reset/([^/]+)/?$  password-reset.php?token=$1   [NC,L] #handle requests for password-reset.php with the token included in a nicer-looking url
</IfModule>

ErrorDocument 404 /404.php

Basically any of the rules that has a hyphen, i.e manage-site, gives me a 404 error UNLESS, I capitalize it, i.e Manage-site -- That works fine.

Any ideas?

Upvotes: 2

Views: 223

Answers (1)

MrWhite
MrWhite

Reputation: 45889

This could happen if MultiViews (mod_negotiation) is enabled for this directory. Try disabling MultiViews by adding the following to the top of your .htaccess file:

Options -MultiViews

With regards to the code in the question, a scenario in which this might fail (ie. result in a 404) is when you are requesting a URL with a trailing slash and a PHP file exists with the same basename and AcceptPathInfo is Off. eg example.com/main/ should otherwise rewrite to /main.php, but instead gets rewritten to /main.php/ (by mod_negotiation) which results in a 404.

MultiViews is often enabled by default on many servers, however, a default Apache install should not have this enabled out of the box.

With MutliViews enabled, Apache tries to map a non-existent file with a file on the filesystem by testing various file extensions (that would return the appropriate mime-type). eg. Request /main (which doesn't exist) it will try /main.php - success. However, this runs before mod_rewrite, so if MultiViews kicks in, the mod_rewrite rule will never match.

In the scenario mentioned above, /main/ also triggers mod_negotiation, internally rewriting the request to /main.php/ (the trailing slash is still trailing). But if AcceptPathInfo is Off this will trigger a 404. This prevents mod_rewrite from rewriting the URL.

By capitalising the request, MultiViews fails (I assume you are on a case-sensitive OS, eg. Linux?), but since you have the NC flag on the RewriteRule this now works.

Upvotes: 2

Related Questions