XAMPP and RewriteRules

Okay, I’m stumped. I’ve tried everything I can think of, but my rewrite rule continues to produce a 404 response instead of routing it to the index.php properly.

Here is my rule (I also tried .*) – same result:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)$ index.php?page=$1 [L]

Here is the log detail:

[Wed Mar 20 13:48:49.867408 2013] [rewrite:trace3] [pid 7648:tid 1756] mod_rewrite.c(467): [client 127.0.0.1:15977] 127.0.0.1 - - [localhost/sid#3a5c50][rid#2a82438/initial] [perdir C:/mysite/] strip per-dir prefix: C:/mysite/about -> about, referer: http://localhost/index.php?page=contact
[Wed Mar 20 13:48:49.867408 2013] [rewrite:trace3] [pid 7648:tid 1756] mod_rewrite.c(467): [client 127.0.0.1:15977] 127.0.0.1 - - [localhost/sid#3a5c50][rid#2a82438/initial] [perdir C:/mysite/] applying pattern '^([a-z]+)$' to uri 'about', referer: http://localhost/index.php?page=contact
[Wed Mar 20 13:48:49.868408 2013] [rewrite:trace4] [pid 7648:tid 1756] mod_rewrite.c(467): [client 127.0.0.1:15977] 127.0.0.1 - - [localhost/sid#3a5c50][rid#2a82438/initial] [perdir C:/mysite/] RewriteCond: input='C:/mysite/about' pattern='!-f' => matched, referer: http://localhost/index.php?page=contact
[Wed Mar 20 13:48:49.881410 2013] [rewrite:trace4] [pid 7648:tid 1756] mod_rewrite.c(467): [client 127.0.0.1:15977] 127.0.0.1 - - [localhost/sid#3a5c50][rid#2a82438/initial] [perdir C:/mysite/] RewriteCond: input='C:/mysite/about' pattern='!-l' => matched, referer: http://localhost/index.php?page=contact
[Wed Mar 20 13:48:49.882410 2013] [rewrite:trace4] [pid 7648:tid 1756] mod_rewrite.c(467): [client 127.0.0.1:15977] 127.0.0.1 - - [localhost/sid#3a5c50][rid#2a82438/initial] [perdir C:/mysite/] RewriteCond: input='C:/mysite/about' pattern='!-d' => matched, referer: http://localhost/index.php?page=contact
[Wed Mar 20 13:48:49.882410 2013] [rewrite:trace2] [pid 7648:tid 1756] mod_rewrite.c(467): [client 127.0.0.1:15977] 127.0.0.1 - - [localhost/sid#3a5c50][rid#2a82438/initial] [perdir C:/mysite/] rewrite 'about' -> 'index.php?page=about', referer: http://localhost/index.php?page=contact
[Wed Mar 20 13:48:49.882410 2013] [rewrite:trace3] [pid 7648:tid 1756] mod_rewrite.c(467): [client 127.0.0.1:15977] 127.0.0.1 - - [localhost/sid#3a5c50][rid#2a82438/initial] split uri=index.php?page=about -> uri=index.php, args=page=about, referer: http://localhost/index.php?page=contact
[Wed Mar 20 13:48:49.882410 2013] [rewrite:trace3] [pid 7648:tid 1756] mod_rewrite.c(467): [client 127.0.0.1:15977] 127.0.0.1 - - [localhost/sid#3a5c50][rid#2a82438/initial] [perdir C:/mysite/] add per-dir prefix: index.php -> C:/mysite/index.php, referer: http://localhost/index.php?page=contact
[Wed Mar 20 13:48:49.882410 2013] [rewrite:trace1] [pid 7648:tid 1756] mod_rewrite.c(467): [client 127.0.0.1:15977] 127.0.0.1 - - [localhost/sid#3a5c50][rid#2a82438/initial] [perdir C:/mysite/] internal redirect with C:/mysite/index.php [INTERNAL REDIRECT], referer: http://localhost/index.php?page=contact

Obviously appending ?page=pageName works as expected. But /pageName is not working… although I’m 99% certain it should.

As best as I can tell from both your rule and your logs, it should work. It seems like there must be something else going on… other htaccess code, or other changes in your main configuration.

You can try to post everything that might be a factor, or… the way I troubleshoot these kinds of things is to restore my config to its original defaults, then add my changes back one by one until I discover the part that’s making things behave badly.

I started with the generic config and added my changes to enable rewrite rules, then I created the htaccess as shown above… sigh

Here are what I feel are the relevant parts (keep in mind, I altered the logs to hide some of the real directory names, I’ll do my best to do that with these too).

httpd.conf (changed the listening port, servername, loglevel)

Listen 23445
ServerName localhost:23445
LogLevel warn mod_rewrite.c:trace8

httpd-xampp.conf (added alias and directory)

    Alias /mysite "/mysite/"
    <Directory "/mysite">
        AllowOverride All
        Require all granted
    </Directory>

To my knowledge, that is all that I changed. I’m at a lost, unless it is related to my Directory settings.

Okay, so adding RewriteBase /mysite to my .htaccecss allows it to work, is there a better alternative? I really don’t want that inside my htaccess (as I have two different environments with two different base names).

So I’m learning a lot this afternoon, it seems because I used Alias, I need RewriteBase (somewhere in the Apache documentation is the following, though I never found it, I did find this quote elsewhere on the web though)

This directive is required when you use a relative path in a substitution in per-directory (htaccess) context unless either of the following conditions are true:

The original request, and the substitution, are underneath the DocumentRoot (as opposed to reachable by other means, such as Alias).

The filesystem path to the directory containing the RewriteRule, suffixed by the relative substitution is also valid as a URL path on the server (this is rare).

As previously mentioned, in other contexts, it is only useful to make your rule shorter. Moreover, also as previously mentioned, you can achieve the same thing by placing the htaccess file in the subdirectory.

So now I need to figure out a different way of doing my development without Alias :slight_smile: I was going to use VirtualHost, but I keep seeing warnings, that VirtualHost is going away…

cp,

Back at your first post, you’re asking [a-z]+ to match pageName. If pageName is a pseudonym of a lowercase pagename, then it should match, otherwise, you’ve either got to match pagename OR change the character range definition to [a-zA-Z]+. What’s the test URI you’re using (if not /pageName as you’d written)?

I didn’t spend much time on the rest after that likely oversight.

Regards,

DK

No, I used all lowercase for my URL testing. Ended up being the use of Alias directive with Directory requires you to use RewriteBase for your RewriteRules. I’m still trying to figure out how to get around that (hoping a new day will help with that :)).