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.

I had mantis setup as www.example.com/mantis and now I've setup my httpd.conf to have it accessible at: mantis.example.com . Since I have many links referencing the earlier links, I need to redirect them to the new location.

This is what I have as my httpd.conf now:

<VirtualHost MYSERVERIP:80>
    ServerAlias EXAMPLE.com
    DocumentRoot /var/www/html/beta
    ServerName EXAMPLE.com
    UseCanonicalName On

 <IfModule mod_rewrite.c>
    RewriteEngine On

RewriteLogLevel 5
RewriteLog "/var/www/html/htaccess.log"

RewriteRule ^/mantis/(.*) http://mantis.EXAMPLE.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
...
 </IfModule>
</VirtualHost>

...

<VirtualHost MYSERVERIP:80>
    ServerAlias mantis.EXAMPLE.com
    DocumentRoot /var/www/html/mantis
    ServerName mantis.EXAMPLE.com
    UseCanonicalName On
</VirtualHost>

However, accessing http://www.EXAMPLE.com/mantis/view.php?id=17575 leads to:

http://guide.opendns.com/main?url=www.mantis.EXAMPLE.com%2Fview.php%3Fid%3D17575 

which shows:

You tried to visit www.mantis.EXAMPLE.com, which is not loading.

Note the www. part. I dont understand where its coming from!!

The htaccess logs are as follows:

MYCLIENTIP - - [27/May/2011:09:40:57 +0000] [EXAMPLE.com/sid#2b4fab862578][rid#2b4fabeec150/initial] (2) init rewrite engine with requested uri /mantis/view.php
MYCLIENTIP - - [27/May/2011:09:40:57 +0000] [EXAMPLE.com/sid#2b4fab862578][rid#2b4fabeec150/initial] (3) applying pattern '^/mantis/(.*)' to uri '/mantis/view.php'
MYCLIENTIP - - [27/May/2011:09:40:57 +0000] [EXAMPLE.com/sid#2b4fab862578][rid#2b4fabeec150/initial] (2) rewrite '/mantis/view.php' -> 'http://mantis.EXAMPLE.com/view.php'
MYCLIENTIP - - [27/May/2011:09:40:57 +0000] [EXAMPLE.com/sid#2b4fab862578][rid#2b4fabeec150/initial] (2) explicitly forcing redirect with http://mantis.EXAMPLE.com/view.php
MYCLIENTIP - - [27/May/2011:09:40:57 +0000] [EXAMPLE.com/sid#2b4fab862578][rid#2b4fabeec150/initial] (1) escaping http://mantis.EXAMPLE.com/view.php for redirect
MYCLIENTIP - - [27/May/2011:09:40:57 +0000] [EXAMPLE.com/sid#2b4fab862578][rid#2b4fabeec150/initial] (1) redirect to http://mantis.EXAMPLE.com/view.php?id=17575 [REDIRECT/301]

Related: httpd.conf changes to allow www.mysite.com and www.mysite.com/mantis and phpmyadmin?


Edit 1

After changing httpd.conf to:

RewriteLogLevel 5
RewriteLog "/var/www/html/htaccess.log"

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^/mantis/(.*) http://mantis.EXAMPLE.com/$1 [L,R=301]

No luck still.

The log file is as:

MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (2) init rewrite engine with requested uri /mantis/view.php
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (3) applying pattern '^' to uri '/mantis/view.php'
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (4) RewriteCond: input='www.EXAMPLE.com' pattern='!^www\.' => not-matched
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (3) applying pattern '^/mantis/(.*)' to uri '/mantis/view.php'
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (2) rewrite '/mantis/view.php' -> 'http://mantis.EXAMPLE.com/view.php'
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (2) explicitly forcing redirect with http://mantis.EXAMPLE.com/view.php
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (1) escaping http://mantis.EXAMPLE.com/view.php for redirect
MYCLIENTIP - - [27/May/2011:10:41:12 +0000] [EXAMPLE.com/sid#2b4fab856518][rid#2b4fabf06640/initial] (1) redirect to http://mantis.EXAMPLE.com/view.php?id=17575 [REDIRECT/301]
share|improve this question

4 Answers 4

up vote 1 down vote accepted

Sigh... figured out the culprit...

There was an .htaccess in /mantis/ that was messing things up... :(

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Just removed it and all working fine now!

share|improve this answer

Do you have NameVirtualHost MYSERVERIP directive in there? If not, I'm pretty sure adding that line to the top will fix your problem. Without it apache just ignores the host header and uses the first matching virtual host it finds based on IP, which is why its re-adding the www from the first vhost rewriterule.

share|improve this answer
    
I'm a little confused about what you say happens, but I checked and i have NameVirtualHost MYSERVERIP:80 . What else can be the problem? @opsguy –  siliconpi May 27 '11 at 11:08
    
Somehow, the first virtualHost is matching the redirected (mantis.EXAMPLE.com) url. It matches the first time on example.com/mantis, throws the 301 to mantis.example.com, but then the first virtualhost somehow is still catching that request and then applies the www rewrite rule, which is why you see that final URL. Are there any wildcards (*) in the first virtualHost serverName or or severAlias lines that aren't shown above? –  opsguy May 27 '11 at 11:26
    
it seems like that's whats happening but there isnt a wildcard next to VirtualHost anywhere . –  siliconpi May 27 '11 at 12:14
    
There are other VirtualHost entries, but they are not conflicting with this –  siliconpi May 27 '11 at 12:17
    
if the /var/www/html/mantis folder is renamed to something else, the redirection stops at the correct place: mantis.EXAMPLE.com/view.php?id=17575 (but obviously, the page doesnt load) –  siliconpi May 27 '11 at 12:27

Well, examine this block here:

RewriteRule ^/mantis/(.*) http://mantis.EXAMPLE.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

First line is correct.

Second gets the HTTP_HOST, which is now mantis.EXAMPLE.com.

Then the fourth line rewrites it to www.mantis.EXAMPLE.com

Easiest way to fix is to move the first line to underneath the fourth. Simple as that.

share|improve this answer
    
hi @theo - thanks for the input, but it still doesnt work - see edit 1. –  siliconpi May 27 '11 at 10:49

Try something like this, more simple.

<VirtualHost *:80>
    ServerAlias example.com

    Redirect /mantis http://mantis.example.com

    ...
</VirtualHost>

<VirtualHost *:80>
    ServerAlias mantis.example.com

    ...
</VirtualHost>
share|improve this answer
    
will it redirect existing queries properly? –  siliconpi May 27 '11 at 12:38
    
no luck - tried it out - same thing anyway... –  siliconpi May 27 '11 at 12:40
    
@matt74tm - Can you try curl -LI http://www.example.com/mantis/view.php?id=17575 to trace what's happening? –  erickzetta May 27 '11 at 12:52

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.