After spending a whole 2 hours of my life trying to get Apache mod-rewrite rules working with XAMPP on a local computer, I thought I’d share my results as I seemingly tried everything. The problem, I have a simple mod-rewrite rule in my htaccess file. When I upload this to my online web host everything is fine – the working htaccess file for my online host:
RewriteBase / RewriteEngine on RewriteRule amnesia/resetpass(.*) recover-password.php$1 [PT]
So typing in www.domain.com/amnesia/resetpass
does a simple re-write to www.domain.com/recover-password.php
, without the user ever knowing. All is fine. However, when I treid to get this seemingly simple rule to work with XAMPP I ran into problems, getting 404 and 500 responses from the server – obviously quite a pain as this essentially means I can’t test the site using my own web server (E.g. localhost). The site hosted from my computer via the normal setup E.g. xampp/htdocs/mysite. I’ll jump straight to the solution and then explain exactly what things were changed – the working htaccess file is below:
RewriteEngine on RewriteBase /mysite options +FollowSymLinks RewriteRule amnesia/resetpass(.*) recover-password.php$1 [PT]
Firstly, the extra line that uses the +FollowSymLinks
directive was added. To explain this I’ll quote straught from the Apache documentation:
To enable the rewriting engine for per-directory configuration files, you need to set “
RewriteEngine On
” in these files and “Options FollowSymLinks
” must be enabled. If your administrator has disabled override ofFollowSymLinks
for a user’s directory, then you cannot use the rewriting engine. This restriction is needed for security reasons.
The re-write base has been changed to the relative path of the website directory. To finish up, open the http.conf file (the default settings for XAMPP, that get overwritten with you .htaccess file rules on a directory basis), located by default at C:\xampp\apache\conf\http.conf. Find all occurances of AllowOverride None
and change it to AllowOverride All
. After restarting XAMPP everythign should work. In a nutshell changing the AllowOverride directive in the http.conf file decalres which directives in .htaccess files can override directives from httpd.conf, this is discussed in more dept over here, but basically by having this directive set to None, you’re stopping individual htaccess files from working locally.
Oh man, thank you!!
it’s not working in my localhost. I’m using 1.7.
I want to make url like http://localhost/lw2/events.php to just http://localhost/lw2/events
How can do that? My .htaccess has these lines
RewriteEngine on
RewriteBase /lw2
options +FollowSymLinks
RewriteRule events(.*) events.php$1 [PT]
ErrorDocument 404 /lw2/notfound.php
im getting 500 responses too 🙁
I’d advise that you check your setup, as these are definately working. What does your error log say?
Thank you, you just saved 2 hours of my life.
Wow, that just saved my ass. Was about to give up trying to get nice web addresses and just stick with plain old php files.
Well done, guy! You’ve just solved an annoying issue I’ve had… I was always forced to test my rewrite rules online, now I can do it on localhost too. I’ll site this article in my personal Google Plus page! Thanks a lot.
Brilliant! It worked right away for me. I thought I would play around with explicitly permitting access to my sub-directory with it’s own block. After I made my changes it no longer worked. So I changed it back and it still didn’t work. I then looked at my http requests and narrowed the problem down to my browser. Looking further I found that browsers cache 301 redirects. Disabling the cache via the Web Developer Tools for FireFox did the trick. While a 301 redirect is specified a ‘cacheable’, it opens a whole discussion on best practices and it’s use by developers. Without delving more on the philosophy I thought I would focus on the practicality and share what worked for me. I started looking into Apache’s mod_headers and setting the cache-control directive to force a re-validation , but it is late and I have a solution in hand. Hope I can save someone’s time.
source: https://support.mozilla.org/en-US/questions/848678
source: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2
Pls help i have tired several htaccess but none is responding tired yours also but still d same im getting server error here is the code
RewriteEngine on
RewriteCond%{REQUEST_FILENAME}!-d
RewriteCond%{REQUEST_FILENAME}.php-f
RewriteRUle^(.*)$ $1.php
Without testing this, it looks liek you’re missing some spaces. Try
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}! -d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRUle^(.*)$ $1.php
Please i have tired using your htaccess but its showing my server error on localhost,im using xampp
Thanks, this way, i troubleshoot problem:
1. Go to the directory of installation C:\xampp\apache\conf
2. Open and edit httpd.conf in a text editor
3. Find the line which contains
#LoadModule rewrite_module modules/mod_rewrite.so
and (uncomment) change to
LoadModule rewrite_module modules/mod_rewrite.so
4. Find all occurrences of
AllowOverride None
and change to
AllowOverride All
I think it appears 2 or 3 times on the configuration file.
5. Restart xampp
How can I write .htaccess file if I am working on local host