Problems with default app running on non-standard port

I have created a default app using yiic and configured apache to run this app on port 8088. So my contact form url (working) looks like this: http://127.0.0.1:8088/site/contact.htm. But when I fill this form and press ‘submit’, it redirects me to http://127.0.0.1/site/contact.htm (discards the port) which obviously doesn’t work because there’s nothing running on port 80 on my machine. If I manually add port number to the url, it successfully displays a flash message “thanks for contacting us”.

Could you please show the view code and the generated HTML code (mainly about the form tag part)?

I didn't change a bit in the generated app - it is all default, except few tweaks of urlManager component:

		'urlManager'=>array(


			'urlFormat'=>'path',


			'showScriptName'=>false,


			'urlSuffix'=>'.htm',


		),


I need to see the generated HTML code.

Here is the latest entry in the apache access log:

Quote

127.0.0.1 - - [02/Feb/2009:13:44:54 -0800] “POST /site/contact.htm HTTP/1.1” 302 -

So it looks like it does redirect to url which doesn't exist.

Here's the source code of contacts page (really, it's default code, you can see it yourself after running yiic):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">


<head>


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


<meta name="language" content="en" />


<link rel="stylesheet" type="text/css" href="/css/main.css" />


<link rel="stylesheet" type="text/css" href="/css/form.css" />


<script type="text/javascript" src="/assets/84074fd5/jquery.js"></script>


<title>My Web Application - Contact Us</title>


</head>





<body>


<div id="page">





<div id="header">


<div id="logo">My Web Application</div>


<div id="mainmenu">


<ul>


<li><a href="/site/index.htm">Home</a></li>


<li><a class="active" href="/site/contact.htm">Contact</a></li>


<li><a href="/site/login.htm">Login</a></li>


</ul>


</div><!-- mainmenu -->


</div><!-- header -->





<div id="content">





<h1>Contact Us</h1>








<p>


If you have business inquries or other questions, please fill out the following form to contact us. Thank you.


</p>





<div class="yiiForm">





<p>


Fields with <span class="required">*</span> are required.


</p>





<form action="/site/contact.htm" method="post">





<div class="simple">


<label for="ContactForm_name" class="required">Name <span class="required">*</span></label><input name="ContactForm[name]" id="ContactForm_name" type="text" value=""/></div>


<div class="simple">


<label for="ContactForm_email" class="required">Email <span class="required">*</span></label><input name="ContactForm[email]" id="ContactForm_email" type="text" value=""/></div>


<div class="simple">


<label for="ContactForm_subject" class="required">Subject <span class="required">*</span></label><input size="60" maxlength="128" name="ContactForm[subject]" id="ContactForm_subject" type="text" value=""/></div>


<div class="simple">


<label for="ContactForm_body" class="required">Body <span class="required">*</span></label><textarea rows="6" cols="50" name="ContactForm[body]" id="ContactForm_body"></textarea></div>





<div class="simple">


	<label for="ContactForm_verifyCode">Verification Code</label>	<div>


	<img id="yw0" src="/site/captcha.htm" alt=""/>	<input name="ContactForm[verifyCode]" id="ContactForm_verifyCode" type="text" value=""/>	</div>


	<p class="hint">Please enter the letters as they are shown in the image above.


	<br/>Letters are not case-sensitive.</p>


</div>





<div class="action">


<input type="submit" name="yt1" value="Submit"/></div>





</form>


</div><!-- yiiForm -->


</div><!-- content -->





<div id="footer">


Copyright &copy; 2009 by My Company.<br/>


All Rights Reserved.<br/>


Powered by <a href="http://www.yiiframework.com/">Yii Framework</a>.</div><!-- footer -->





</div><!-- page -->


<script type="text/javascript">


/*<![CDATA[*/


jQuery(document).ready(function() {


jQuery('img#yw0').after("<a href="#" id="yt0">Get a new code</a>");


jQuery('#yt0').click(function(){jQuery.ajax({'success':function(html){jQuery("#yw0").attr("src",html)},'url':'/site/captcha/refresh/1.htm','cache':false});return false;});


});


/*]]>*/


</script>


</body>





</html>

It looks fine to me. The form action URL is correct which is a relative URL, meaning using the current host info (including host name and port number).

I also couldn't reproduce the issue. Maybe you should check you .htaccess or Web server setup?

.htaccess was copied from yii guide:

Options +FollowSymLinks


IndexIgnore */*


RewriteEngine on





# if a directory or a file exists, use it directly


RewriteCond %{REQUEST_FILENAME} !-f


RewriteCond %{REQUEST_FILENAME} !-d





# otherwise forward it to index.php


RewriteRule . index.php

Apache was configured with virtual host for this app:

Listen 8088


NameVirtualHost 127.0.0.1:8088


<VirtualHost 127.0.0.1:8088>


ServerName www.myservername.com


DocumentRoot "C:/Program Files/Apache/htdocs/MyTestApp"


</VirtualHost>

What else can I check? Everything works absolutely fine if I change 8088 to 80 and restart Apache. But when I change port back, it stops working with the behavior I described. So between two states (working vs non-working) the only difference is port number (80 vs 8088). All other configuration stays the same.

The problem appears to be with CHttpRequest::redirect which redirects to "http://127.0.0.1/site/contact.htm" instead of "http://127.0.0.1:8088/site/contact.htm".

You wouldn't believe that, but here are two $_SERVER variables:

_SERVER["HTTP_HOST"] = 127.0.0.1:8088


_SERVER["SERVER_PORT"] = 80

Since CHttpRequest uses SERVER_PORT variable, it redirects to the wrong url. Looks like a bug in Apache or PHP?

I have added the following code to CHttpRequest::getHostInfo method after $port assignment:

if (count($segs) == 2 && $port != $segs[1])


    $port = $segs[1];


Could you please apply this patch to Yii?

I see. Thanks. Fixed.