.htacces Question

I’m using WAMP to serve a pet project on my localhost called “mvc.” I’m trying to redirect requests made to mvc’s root to a sub folder called /public. I have a .htaccess file in my root with the below code. It’s not getting the job done.

RewriteEngine on
#!-d .htaccess ignores all requests made directly to directories sub folders
RewriteCond %{REQUEST_FILENAME} !-d
#!-f .htaccess ignores all requests made to specific files within directory
RewriteCond %{REQUEST_FILENAME} !-f
#favicon.ico is a default file some browsers request. don't let browser trigger 404 if we don't have this file
RewriteCond %{REQUEST_URI} !=/favicon.ico

RewriteBase /mvc/public

#send all alphanumeric requests to index.php as QSA, second match catches file extension
RewriteRule ^([-_A-z0-9\\/]+)\\.?([A-z]+)?$ /mvc/public/index.php?url=$1&extension=$2 [QSA,L]

I have mod_rewrite enabled and I’m not getting any errors.




[A-z] is shorthand for [A-Za-z] which works just as well and makes your code more readable. About the extension being optional, this is correct. I’m bootstrapping everything through the directory’s index.php file.

It may not have been the cause of your problem, but I think this trick is actually bad practice, because it deceptively includes characters you may not have intended, such as [ \ ] ^ _ `

On to your issue. I copy-pasted your htaccess, but it worked correctly for me.

To make sure we’re on the same page, here is where the files were set up.


I wasn’t aware of this. Thank you for pointing that out.

Your directory tree is correct. That is how I have my project set up. After stating you were able to run the script successfully, I had to ensure mod_rewrite was in fact enabled. It is. The rewrite_module is being loaded and the AllowOverride rules are set accordingly.

If anything, thank you at least for pointing out the specifics with the regex flaw. That may be the most important thing I learn all day!