0

I need to redirect http://1.2.3.4/foo to http://1.2.3.4/WebFOO. The rule that I've created looks like this in ApacheRoot\conf\httpd.conf:

RewriteEngine On
RewriteRule ^/?foo /WebFOO [R,L]

However, this approach doesn't work even though this rule is syntactically correct. Means I connect to http://1.2.3.4/foo and get a 404 or the specific content for "/foo". Note: to test things out I've created a folder with a simple index.html in (htdocs/foo). This actually prevented the 404 of course but still the redirection did not take place.

The virtual-host portion looks like this and is beneath the mod-rewrite rule:

ServerName 1.2.3.4
<VirtualHost *:80>
  ServerName 1.2.3.4:80
  #Redirect permanent / https://1.2.3.4/
</VirtualHost>

Why is that redirection completely ignored? Am I able to check somehow whether the server at least recognizes the rule (no rewrite-entries so far in the error.log)?

Note: To be sure about the syntactical correctness of the rule I've also tested it with http://htaccess.madewithlove.be:

Rewrite rule tester result

Not only this test worked fine but also things like /foo/index.html. It was correctly translated into /WebFOO/index.html.

Note: Enabling the verbose logging as suggested by one of a mod_rewrite canonical question's answer is not suitable for apache 2.4:

For more complex rules, use mod_rewrite's RewriteLog directive to log activity to a file and set RewriteLogLevel 3

Since Apache httpd 2.4 mod_rewrite RewriteLog and RewriteLogLevel directives has been completely replaced by the new per-module logging configuration.

LogLevel alert rewrite:trace6

RewriteLog

Those familiar with earlier versions of mod_rewrite will no doubt be looking for the RewriteLog and RewriteLogLevel directives. This functionality has been completely replaced by the new per-module logging configuration mentioned above. To get just the mod_rewrite-specific log messages, pipe the log file through grep: tail -f error_log|fgrep '[rewrite:'

  • I don't have any [rewrite: entries in my ApacheRoot\logs\error.log
OddDev
  • 111

2 Answers2

0
RewriteRule ^/foo$ WebFOO [R]

Try the following instead:

RewriteRule ^/?foo /WebFoo [R,L]
MrWhite
  • 13,315
0

First of all why do you define the RewriteRule outside of the vHost? Try to define the Rule inside the vHost. I'd also correct the ServerName inside the vHost to:

ServerName 1.2.3.4

Also the LogLevel part should be inside the vHost. To see if the Rewrite is working check with the Chrome Developer Tools, There you can check if you get a 301. Maybe you get the 404 after the redirect?

harp
  • 101
  • 1