Question

I have the following in my .htaccess to redirect non-www to www:

RewriteCond %{HTTP_HOST} !^(.*)\.mysite\.com$ 
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]

This works fine, but a visit to www.mysite.com:8080 redirects to www.mysite.com removing the above lines from my .htaccess file results in the visit to port 8080 working.

So I know it is these two lines causing the issue.

I want to still have this affect, non-www redirected to www, but I want the redirect to maintain the port.

I have tried various versions of the above using %{SERVER_PORT} but had no luck, they either do not work or result in a redirect loop.

Thanks Jake

EDIT

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php/$1 [L]

EDIT 2

I have now tried this:

RewriteCond %{SERVER_PORT} ^8080$
RewriteCond %{HTTP_HOST} !^(.*)\.mysite\.com:8080$ 
RewriteRule ^(.*)$ http://www.mysite.com:8080/$1 [R=301,L]

In an attempt to redirect a request on port 8080 to port 8080 again, this fails and redirects to www.mysite.com with no port. What does be useful would be just do nothing if the port is 8080, as this is only for testing and so I don't need to non-www/www redirect, e.g.

RewriteCond %{SERVER_PORT} ^8080$
RewriteRule [Break here and do nothing more]
Was it helpful?

Solution

Answering my own question here. But basically attempting to direct the port in .htaccess was not going to work.

I am using Varnish and wanting to manipulate the request, but because Varnish is on one port and Apache is on another it was hard to tell was is actually going on here.

In short I gave up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top