Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

The same Apache rewrite rule works (match) in VHost but not in ".htaccess" file:

<VirtualHost *:80>
ServerName 192.168.1.100
ServerAlias example.com
DocumentRoot /test

RewriteEngine On
RewriteRule ^/test.html$ /test2.html
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9

<Directory "/">
AllowOverride All
</Directory>

</VirtualHost>

Rewrite Log:

192.168.1.10 - - [13/May/2013:16:19:31 +0100] [example.com/sid#7fd764555ed0][rid#7fd764ad9758/initial] (2) init rewrite engine with requested uri /test.html
192.168.1.10 - - [13/May/2013:16:19:31 +0100] [example.com/sid#7fd764555ed0][rid#7fd764ad9758/initial] (3) applying pattern '^/test.html$' to uri '/test.html'
192.168.1.10 - - [13/May/2013:16:19:31 +0100] [example.com/sid#7fd764555ed0][rid#7fd764ad9758/initial] (2) rewrite '/test.html' -> '/test2.html'
192.168.1.10 - - [13/May/2013:16:19:31 +0100] [example.com/sid#7fd764555ed0][rid#7fd764ad9758/initial] (2) local path result: /test2.html
192.168.1.10 - - [13/May/2013:16:19:31 +0100] [example.com/sid#7fd764555ed0][rid#7fd764ad9758/initial] (2) prefixed with document_root to /test/test2.html
192.168.1.10 - - [13/May/2013:16:19:31 +0100] [example.com/sid#7fd764555ed0][rid#7fd764ad9758/initial] (1) go-ahead with /test/test2.html [OK]

".htaccess file"

# cat /test/.htaccess 
RewriteEngine On
RewriteRule ^/test.html$ /test2.html

Rewrite Log:

192.168.1.10 - - [13/May/2013:16:22:17 +0100] [example.com/sid#7fd76455b7d0][rid#7fd764af0d78/initial] (3) [perdir /test/] strip per-dir prefix: /test/test.html -> test.html
192.168.1.10 - - [13/May/2013:16:22:17 +0100] [example.com/sid#7fd76455b7d0][rid#7fd764af0d78/initial] (3) [perdir /test/] applying pattern '^/test.html$' to uri 'test.html'
192.168.1.10 - - [13/May/2013:16:22:17 +0100] [example.com/sid#7fd76455b7d0][rid#7fd764af0d78/initial] (1) [perdir /test/] pass through /test/test.html
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

According the second log, it's looking for "^/test.html$" in "test.html" URL. Cannot match it because of forward slash in pattern. Try

RewriteRule ^test.html$ /test2.html

instead of

RewriteRule ^/test.html$ /test2.html

in your .htaccess file.

share|improve this answer
add comment

Add this above the RewriteRule in your .htaccess:

RewriteCond %{HTTP_HOST} ^(www.)?/test/test.html$

Change the path to suit your environment.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.