I’m currently trying to reconfigure a Apache server to be able to use another port than 80 when accessing a webpage, and then depending on what port a request was done I do some .htaccess magic. There is no problems with reconfiguring the Vhost and listen to any other port, but for some reason all non-ssl requests are always interpreted as coming to port 80.
If I try to open the page my.site.com:8080 my log shows something like this
my.site.com:80 my.ip. – – [23/Jan/2012:14:37:24 +0100] “GET
/images/pagenav_bg.gif HTTP/1.1” 200 2484
“http://my.site.com:8080/css/all.css” “Mozilla/5.0 (Windows NT 6.1;
WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1”
My .htaccessrules that have the condition “RewriteCond %{SERVER_PORT} ^80$” kicks in, and PHP $_SERVER[‘SERVER_PORT’]; shows port 80.
I’ve tried with different ports and I’ve even disabled listening on port 80, which gives the expected result that my.site.com stops working, but my.site.com:8080 still works.
So to be clear about the questing: How do I enable/reconfigure apache so that the “real” port number is used?
Edit: I just found out that this might have to do with mod_rewrite. But I haven’t found out how to circumvent it. I also find it a bit strange that .htaccess interpretates the port as 80 even before any rewrite rules run.
Edit2: After some more experimenting I’ve deducted that the problem is probably in my .htaccess file.
At the moment it looks like this
RewriteEngine On
#Rewrite rule to allow normal access to existing files
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
#The normal rewriterule for the framework that is used
RewriteRule ^.*$ index.php [NC,L]
I believe the following is happening. When the rewrite is done it goes from http://my.site.com:8080/some/url to be interpreted as my.site.com/index.php by Apache, which by default is port 80. I must rewrite it to my.site.com:8080/index.php instead. The following snipped does not work however.
RewriteCond %{SERVER_PORT} ^8080$
RewriteRule ^(.*)$ http://my.site.com:%{SERVER_PORT}/index.php [NC,L]
I’ve found the answer and the redirects now work correctly. Unfortunately this means that I have to hard-code my redirects, but in this case it is acceptable.
The problem was, as described in the edit to my question above, Apache and the .htaccess where my.site.com:8080/some/url was rewritten to my.site.com/index.php, and not my.site.com:8080/index.php.
this was solved by adding one rule for each port where a specific case was needed such as
As seen in my edits in the original questions, this did not work at first, but that was only because my rules came in the wrong order. Problem solved.