URL Rewriting and mod_rewrite Not Working? Read On!
Okay, so I was trying to use mod_rewrite on Apache. That’s the program you use to take a URL like this: /photos/viewphoto?photoid=5 and turn it into something pretty like: /photos/viewphoto/5. It makes your URLs look better on the surface, but underneath the server still gets to play with all the messy query strings that keep your database-backed site humming.
So anyway, I was trying to do the example shown above, and every online tutorial showed me that I should put these lines in my .htaccess file:
RewriteEngine on
RewriteRule viewphoto/([0-9]+) viewphoto.html?id=$1 [NC]
But it was not working. Just was not. I would go to /photos/viewphoto/5, and get the viewphoto.html page with no query string attached to it. What could be wrong?
On a whim once, I changed the URL requested to /photos/viewPhoto/5. Notice the capital P? That made it work. But why? Why would it work with a capital letter, but not all in lower case?
After much walking around and banging my head, I finally figured out the reason. mod_rewrite only works if you’re getting a 404 error from your URL. When I requested /photos/viewphoto/5, the server could see that there was already a file named viewphoto.html. So it just served up that file, and didn’t get mod_rewrite involved at all. What I had to do was change the URL so that I’d get a 404 Page Not Found error if my rewrite rules weren’t there. So I did this:
RewriteEngine on
RewriteRule view/([0-9]+) viewphoto.html?id=$1 [NC]
And requested the page like so: /photos/view/5. Now, because there was no view.html, the server sent the request to mod_rewrite, and my rewrites happened like I wanted them to.
So be careful about this if you’re using mod_rewrite. Don’t give your “pretty” URLs a filename that actually exists on the system, or your rewrites will never work.
To see this at work, check out a sneak preview of my new Around Carson website. I’m using it in the photo section.
http://aroundcarson.com/photos/view/1
http://aroundcarson.com/photos/view/2
Filed under The Computer Vet Weblog

Hello! Good Site! Thanks you! nycdtuvivvko
Fine ,Verygood
Actually i was looking a code like this.
Thank god for this post. Custom 404 directives are so common, that I would not have looked for this as a cause for my mod_rewrite not working. I also found that ‘AllowOverride All’ in the section of httpd.conf was needed in my case.
Note: You can add ErrorDocument 404 to your .htaccess file(last command in the file) so that you can continue to have informative error pages for missing files.
Thank Again.