OpenShift Redirect Loop between www and non-www - redirect

I'm experiencing an infinite redirect loop when I try to access my site. The redirect loop seems to be between the non-www version and the www version.
My DNS has a URL Redirect set for http://domain.tld to www.domain.tld. Then www.domain.tld has a CNAME record for app-namespace.rhcloud.com.
For the app I added the alias www.domain.tld
I've also tried to add an alias for domain.tld, but that doesn't help.
Is there something I'm missing? Thank you very much for your help!

I'm hosting a laravel 5 project on openshift. www and non-www are tricky but I finally got it working with a simple 2 part solution...
since you can't use non-www for a cname, make it an A-record to 174.129.25.170 and they will add the www. That's all they seem to do. Weird, I know.
Use this in your .htaccess It seems to work better in cloud situations.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I found the issue. It was due to a redirect from www.domain.tld to domain.tld that was caused by a rewrite rule in the .htaccess file:
<IfModule mod_rewrite.c>
#RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
Removing this resolved the issue.

Related

What should I do to fix redirect in cpanel

Im trying to redirect one of my domain to the other one and added a redirect in cpanel with wild card redirect. It has added a line at the end of .htaccess file
RewriteCond %{HTTP_HOST} ^oldDomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.oldDomain\.com$
RewriteRule ^(.*)$ "https\:\/\/newDomain\.com\/$1" [R=301,L]
but its not working correctly if I go to oldDomain.com/about it will redirect to this page https://newDomain.com/cache/wp-rocket/oldDomain.com/about/index-https.html_gzip
what should I do to fix this issue?
I have tried godaddy forward domain

Redirect users from www.mysite to mysite

We've been issued a new certificate for our website, but unfortunately without an alternative name for the www subdomain. Visiting the TLD without www works just fine.
Redirecting them via .htaccess doesn't work, probably because the initial connection isn't even made.
I've tried using this:
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www.mysite$
RewriteRule ^(.*)$ https://mysite/$1 [R]
Is there any way to "force" users away from www or do we need to be issued a new (fixed) certificate for our domain?
Check this redirect (change example.com to your domain):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L]

redirecting a subdomain to a domain using htaccess

I would like to redirect a subdomain to a folder in its main domain. The subdomain is blog.mydomain.com and I would like to redirect it to http://www.mydomain.com/blog.
I have tried to do this using an htaccess file as follows:
RewriteEngine on
Options +FollowSymlinks -MultiViews
RewriteCond %{SERVER_PORT} =80
RewriteCond 5{HTTP-HOST} ^blog.mydomain.com$ [NC]
RewriteRule ^(.*)$ /blog/$1 [PT,L]
but it doesn't work (the redirection does not happen).
Does anyone know how I could do this?
Thank you.
Try with
Redirect 301 / http://www.mydomain.com/blog
If that doesn't work,i'll try something else

redirecting just a single non-www homepage to www version, without redirecting any other pages

I need to redirect the non-www version of just a single page, the homepage, to www.
I want to leave the rest of the site alone. Due to a need to keep a specific page non www.
So if i try a rewrite rule of
/index.html http://www.example.com/
I get a infinite redirect type error.
There's probably a way to achieve what you want, although I must say that this has a bit of a "smell" to it. It sounds like you are trying to treat a single symptom rather than fix a problem (which, admittedly might be beyond your control). Is it possible that your organization needs to setup a domain pointer so that www.example.com and examples.com are the exact same thing?
So you want
http://example.com/ to go to http://www.example.com/,
but you want
http://example.com/index.html,
http://example.com/otherstuff, and
http://example.com/subdir/path/file.php
to stay the same?
For Apache:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/$ http://www.example.com/ [R=301,L]
If you want any non-www subdomain on the site to get redirected to the www version:
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^/$ http://www.%{HTTP_HOST}/ [R=301,L]
If you want to redirect everything except a specific page:
RewriteCond %{REQUEST_URI} !^/subdir/path/file.php$
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

How do I use .htaccess to force www. while using Zend Framework

I want to force a www. prefix on my website by using a .htaccess 301 redirect. I am currently trying:
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
Which normally works, but I am using Zend Framework which causes all requests to be redirected back to http://www.mysite.com/index.php regardless of the initial request.
For example...
http://mysite.com/blog,
http://mysite.com/contact,
http://mysite.com/blog/this-is-my-article,
Will all be redirected to http://www.mysite.com/index.php
However, if I initially request a specific file, such as...
http://mysite.com/some-file.htm
The redirect works properly, redirecting to http://www.mysite.com/some-file.htm
In first time, don't forget to enable the rewriting ("RewriteEngine on").
The last line is important if you use Zend Framework.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteRule !\.(pdf|php|js|ico|txt|gif|jpg|png|css|rss|zip|tar\.gz)$ index.php
Now the url...
http://mysite.com/some-file.htm
... redirect to http://www.mysite.com/some-file.htm but use the index.php
Don't forget to ignore sub-domains when re-directing to www.
http://www.theblogaholic.com/2011/01/16/force-www-using-htaccess-except-for-subdomains/