1

I'm attempting to follow a mod_rewrite tutorial but I can't get a particular example to work. I've enabled mod_rewrite and it is working for simple examples like the following .htaccess.

# Redirect everything in this directory to "good.html" 
RewriteEngine on RewriteRule .* good.html

The following code is the contents of my .htaccess

# Enable Rewriting
RewriteEngine on

# Rewrite user URLs
#   Input:  user/NAME/
#   Output: user.php?id=NAME
RewriteRule ^user/(\w+)/?$ user.php?id=$1

I've had a look around and found out how to log what mod_rewrite was doing and my logs showed the following information

192.168.1.70 - - [03/Jan/1970:20:41:55 +0100] [192.168.1.64/sid#41353678 [rid#417c1518/subreq] (3) [perdir /var/www/] add path info postfix: /var/www/user.php -> /var/www/user.php/bob
192.168.1.70 - - [03/Jan/1970:20:41:55 +0100] [192.168.1.64/sid#41353678][rid#417c1518/subreq] (3) [perdir /var/www/] strip per-dir prefix: /var/www/user.php/bob -> user.php/bob
192.168.1.70 - - [03/Jan/1970:20:41:55 +0100] [192.168.1.64/sid#41353678][rid#417c1518/subreq] (3) [perdir /var/www/] applying pattern '^user/(\\w+)/?$' to uri 'user.php/bob'
192.168.1.70 - - [03/Jan/1970:20:41:55 +0100] [192.168.1.64/sid#41353678][rid#417c1518/subreq] (1) [perdir /var/www/] pass through /var/www/user.php
192.168.1.70 - - [03/Jan/1970:20:41:55 +0100] [192.168.1.64/sid#41353678][rid#417ae4d0/initial] (3) [perdir /var/www/] add path info postfix: /var/www/user.php -> /var/www/user.php/bob
192.168.1.70 - - [03/Jan/1970:20:41:55 +0100] [192.168.1.64/sid#41353678][rid#417ae4d0/initial] (3) [perdir /var/www/] strip per-dir prefix: /var/www/user.php/bob -> user.php/bob
192.168.1.70 - - [03/Jan/1970:20:41:55 +0100] [192.168.1.64/sid#41353678][rid#417ae4d0/initial] (3) [perdir /var/www/] applying pattern '^user/(\\w+)/?$' to uri 'user.php/bob'
192.168.1.70 - - [03/Jan/1970:20:41:55 +0100] [192.168.1.64/sid#41353678][rid#417ae4d0/initial] (1) [perdir /var/www/] pass through /var/www/user.php
192.168.1.70 - - [03/Jan/1970:20:41:55 +0100] [192.168.1.64/sid#41353678][rid#417bb500/subreq] (3) [perdir /var/www/] strip per-dir prefix: /var/www/bob -> bob
192.168.1.70 - - [03/Jan/1970:20:41:55 +0100] [192.168.1.64/sid#41353678][rid#417bb500/subreq] (3) [perdir /var/www/] applying pattern '^user/(\\w+)/?$' to uri 'bob'
192.168.1.70 - - [03/Jan/1970:20:41:55 +0100] [192.168.1.64/sid#41353678][rid#417bb500/subreq] (1) [perdir /var/www/] pass through /var/www/bob

So I understand from the log that the regex is not matching because it is being compared to user.php/bob rather than user/bob. If I change the rewrite rule to ^user.php/(\w+)/?$ then it works. What I don't understand is why Apache is looking for user.php when the href doesn't contain .php, see below.

<a href="user/bob">user/bob</a>

Can anyone explain how to make Apache look for user/bob?

Also, if possible, can anyone explain why this happens?

Community
  • 1
  • 1
derek_user
  • 72
  • 2
  • 7

1 Answers1

1

Looks like multiviews is matching "user" to "user.php" before mod_rewrite even gets applied. Try turning it off:

Options -Multiviews

Multiviews is part of mod_negotiation, and it tries to "guess" a requested file by looking for similarly named files (like files minus the extension). It gets applied before mod_rewrite so it will bypass your rewrite rule.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220