I used xampp in the past in order to develop websites on a local computer, but I recently found it less cumbersome to simply start a server using the PHP -S localserver:8000
where 8000
is the port number. I managed to get phpmyadmin
working that way and access databases. So all was fine ... until I tried to re-use redirecting rules within a .htaccess
file.
The rules work well on the my web provider's servers, but it doesn't work on localhost. I assume there's something to do in order to turn the re-write rules on but since I am not longer using xampp, I don't know if there's still something like Apache running in the background, etc. If someone has experience with this setup, I would like to know how or if this can be be done.
The redirect is basic:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule lessons/(.*)$ page.php?url=$1
And to make the question more meaningful (addressing some of the comments below), I think Finwe answered my question rightly, that is: "this doesn't work with the PHP approach, .htaccess is specific to Apache".
My question wasn't about "what alternative solution do I have". The solution I would use is probably to go through a combination of AJAX/PHP to solve the limitation but that's surely not the most straightforward workaround.
PHP's internal server does not recognize .htaccess files. That is a privilege of using Apache server.
The redirects must be a part of your PHP application to retain them across all platforms.
With PHP internal server, you can run it with a PHP file parameter.
If a PHP file is given on the command line when the web server is started it is treated as a "router" script. The script is run at the start of each HTTP request. If this script returns FALSE, then the requested resource is returned as-is. Otherwise the script's output is returned to the browser.
PHP -S localserver:8000 router.php
The script could then replace the htaccess file checking and "routing" requests to lessons/
.
For more complex applications I would suggest using a "real" webserver such as Apache or nginx as the built-in server has more limitations, such as hanging on external HTTP requests, as it is only single threaded.
This is what I do to get htaccess working properly on localhost. I create a virtual host for each app am building
<VirtualHost *:8000>
ServerName app.local
DocumentRoot "C:/path/htdocs/app"
<Directory "C:/path/htdocs/app">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
With this, I can now access my app from my browser using the local domain name app.local
. I found out that htaccess
works well following this which is apparently the same thing with online server you access with a domain name