Question

EDIT: This issue was mostly solved by an answer below, but it also raised a new question. If I remove "RewriteCond %{REQUEST_FILENAME} !=F" from the .htaccess file, I am again redirected to stock.com/index.php?page=index.php if I make a request to stock.com/register. I could just accept that it needs to be there, but I'm trying to understand why removal of this line of code causes this behavior. I get that it is there to allow visitors access to actual files such as images, but I don't get how it is also related to a redirect/reroute of a URL such as stock.com/register. If anyone could explain that I would really appreciate it.

Trying to get a pretty URLs to work for a simple MVC site I'm developing locally on Wampserver, just for learning purposes. I have the following .htaccess code setup so that any URL request is routed back to the index.php controller, which serves different content based on the $_GET value of 'page'.

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !=D
RewriteCond %{REQUEST_FILENAME} !=F
RewriteRule ^([-\w.]+)$ index.php\?page=$1 [R=302,L]

I have it set to R=302 just so I can see where it's redirecting me. The hostname I've set it up to work at is stock.com. If I go to stock.com, this works fine. But if I try to visit stock.com/register, I'm redirected to stock.com/index.php?page=index.php. If I take the dot out of the regex, this all works fine, but then I'm unable to have page/file names with dots in them. Not necessary, but I'm doing this for learning purposes, so I'd like to know what the solution is here. I've tried both escaping the dot and not. Thanks!

Was it helpful?

Solution

Some syntax issues. Try this code:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([-\w.]+)$ index.php?page=$1 [R=302,L,QSA]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top