9

I run XAMPP and I'm trying to learn how .htaccess works. I have a file structure that looks something like this:

/parent
    /foo
    /bar
    .htaccess

I simply want to change all foo requests to bar with a GET parameter appended after them. For example:

foo/
foo/hello.php

turn into:

bar/?test=yes
bar/hello.php?test=yes

When I try to put the <Directory> directive in my .htaccess file:

<Directory "/foo">
    Options +Indexes
</Directory>

I get the following error log:

[Tue Sep 19 17:23:58.356362 2017] [core:alert] [pid 6336:tid 1900] [client ::1:60018] C:/xampp/htdocs/parent/.htaccess: <Directory not allowed here

I checked my httpd.conf file and everything is alright. I'm sure because if I change the contents of the .htaccess file to simply:

Options -Indexes

It correctly displays a 403 error. If I replace the - with +, it shows the directory listing.

What am I doing wrong?

anubhava
  • 761,203
  • 64
  • 569
  • 643
dodov
  • 5,206
  • 3
  • 34
  • 65

3 Answers3

7

<Directory> directive is not allowed in .htaccess and for meeting your requirements you don't even need this in .htaccess.

You can use this rule in site root (parent) .htaccess:

RewriteEngine On

RewriteCond %{QUERY_STRING} !(?:^|&)test=yes(?:&|$) [NC]
RewriteRule ^foo/(.*)$ bar/$1?test=yes [NC,QSA,L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    I understand what the `RewriteRule` does and what the pattern in it matches, but can you explain what `RewriteCond` does? I can't understand what it checks. – dodov Sep 20 '17 at 08:00
  • 1
    `RewriteCond` uses a negation operator `!` at the start which means execute this rule only when `QUERY_STRING` doesn't already have `test=yes` query parameter. query parameter might be at the start, middle or in end. That is why I have `(?:^|&)` for start/middle and `(?:&|$)` for middle/end positions. – anubhava Sep 20 '17 at 08:02
1

<Directory.. is not allowed in .htaccess according to the manual. You can have it only in server config and virtual host. You should use mod_rewrite instead.

akond
  • 15,865
  • 4
  • 35
  • 55
  • 1
    Can you show an example? How would I implement that to solve my problem? The [manual](http://httpd.apache.org/docs/current/mod/mod_rewrite.html) gives examples with ``. Maybe show an example solution to the problem in my question? – dodov Sep 19 '17 at 18:15
1

Short answer: Just delete the .htaccess file and your error.log problem will vanish.

Longer answer:

I started getting the same message in my C:\xampp\apache\logs\error.log file earlier today. The error began suddenly (after years of using localhost without a hitch) and coincided with a "500 server error" when I tried to start localhost.

The content of the .htcaccess file (which had appeared in my /Sites directory for the first time today) was:

<Directory>
  AllowOverride All
  Order allow,deny
</Directory>

As others have noted in relation to similar .htcaccess questions, the file is not generally necessary.

I therefore simply deleted the .htcaccess file, which instantly solved the problem. Immediately localhost started working again, and the error vanished from the logs!

TechnoCat
  • 667
  • 8
  • 16