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
  • 6
  • Where is this... server config or per-directory / .htaccess file? Any other directives? Define "doesn't work" - error? Nothing? Something else? – MrWhite Sep 12 '16 at 08:31
  • @w3d I've added the information to the question :) Do you need any additional information? – OddDev Sep 12 '16 at 08:34
  • Try removing the `` wrapper... do you get an error? (Unless this is intended to work without mod_rewrite, then you don't need the wrapper.) – MrWhite Sep 12 '16 at 08:49
  • @w3d Hm, very good idea but didn't help :/ I've added the result to the question. Also checked the error-log which also doesn't state anything. – OddDev Sep 12 '16 at 08:54
  • "Note: to test things out I've created a folder with a simple index.html in (htdocs/foo)" - If you've created a _folder_ called `foo` then this may not match since mod_dir (`DirectorySlash` default config) will try to fix the URL by appending a slash (301 redirect - which is likely to be cached). mod_dir generally fires _before_ mod_rewrite, so the URL-path that mod_rewrite sees is likely to end in a slash. – MrWhite Sep 12 '16 at 10:28
  • @w3d Sorry, didn't get that one. Can you try to explain it once again? :) – OddDev Sep 12 '16 at 11:10

2 Answers2

0
RewriteRule ^/foo$ WebFOO [R]

Try the following instead:

RewriteRule ^/?foo /WebFoo [R,L]
MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • Nope did not work... still get a 404 when connecting to https://a.domain/foo – OddDev Sep 12 '16 at 08:36
  • Have you restarted your server? Otherwise, this should be doing _something_, provided it's in the correct place in the config file? Is it near the top of a VirtualHost container for port 443 (HTTPS)? Remove the `` wrapper... do you get an error? – MrWhite Sep 12 '16 at 08:41
  • Yep, I went into services.msc and restarted the apache service via a right-click. Actually it's a q-system and therefore we don't have https up and running. I just wanted to avoid "why do you use http"-discussions^^ However, the virtualhost portion is on line 252 whereas mod_rewrite portion is on 178. I'll update the question. – OddDev Sep 12 '16 at 08:47
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