4

Openshift application url: example.rhcloud.com

Custom domain: example.com

I came across this link and added the redirection rule. However, I can not access example.rhcloud.com now. My custom domain is www.example.com.

To avoid duplicate content problems, I tried this redirection.

Here is my .htaccess file:

RewriteEngine on 

# Uncomment the following lines to force HTTPS
#RewriteCond %{HTTP:X-Forwarded-Proto} !https 
#RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]


# WordPress Defaults
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# Redirect Https traffic to Http
RewriteEngine on 
RewriteCond %{SERVER_PORT} ^443$ [OR]
RewriteCond %{HTTPS} on
RewriteRule  ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

# Redirect non-www to www
RewriteEngine on 
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 


# Redirect openshift rhcloud domain to custom domain
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^example.rhcloud.com$
RewriteRule (.*)$ http://www.example.com/$1 [R=301,L]

Why is example.com not redirecting to ww.example.com? How do I fix this?

Also, I see that since I have https to http redirection and non www to www redirection, my blog has been loading slow. How can I reduce it to lesser number of rules?

Edit:

In chrome: enter image description here

In Firefox:

The page isn't redirecting properly, Firefox has detected that the server is redirecting the request for this address in a way that will never complete. This problem can sometimes be caused by disabling or refusing to accept cookies.

18
  • You may be thinking backwards?? Do you not want to redirect www.example.com to example.rhcloud.com? Isn't example.rhcloud.com where your site is? If this is the case, you do not need a redirect. Just set a CNAME from one to the other.
    – closetnoc
    Oct 7, 2015 at 22:01
  • @closetnoc: I have the Cname set up. However I do not want google to index my content on different urls.This way my content is going to be available on example.rhcloud.com and example.com. I want my content to be at example.com only. Oct 7, 2015 at 22:04
  • Could you please check the link I mentioned in the post once?That explains why i am trying this Oct 7, 2015 at 22:05
  • I would set it up so that example.com is how people know you and use a canonical tag on all of your pages pointing to example.com/whatever-your-page-is so that Google and search engines will get it right without duplicate content issues.
    – closetnoc
    Oct 7, 2015 at 22:08
  • Oh, Thank you. In that case, what are all these pages where I need to set the canonical tag? In my header.php I guess? and apart from that? Oct 7, 2015 at 22:10

1 Answer 1

2

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

If you are redirecting from one completely different domain to another then you would not expect to be able to use %{HTTP_HOST} in the target. For example, the above would result in example.rhcloud.com being redirected to www.example.rhcloud.com - which is not the desired result. Then the following redirect to your actual domain would never match.

All you need is something like the following before the "WordPress" directives. Note that the order of directives in .htaccess is important.

RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=302,L]

This simply redirects to your desired domain if you are not already at your desired domain. There is no need for an explicit canonical www redirect, or checking for HTTPS (your new domain is not accessible by HTTPS).

Clear your browser cache before testing (301 redirects are cached). Change the 302 in the above to 301 when you are sure it's working OK.

So, in summary, your complete .htaccess file should look like:

RewriteEngine On
RewriteBase /

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

# WordPress Defaults
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
24
  • I am using incognito window. I used the one that you mentioned, It gives 503 Service Unavailable No server is available to handle this request. Oct 8, 2015 at 9:28
  • Also favicon.ico:1 GET http://www.example.com/favicon.ico 503 (Service Unavailable) This is what I get in console. Oct 8, 2015 at 9:40
  • You'll need to keep closing and opening the incognito window. It's easier to keep the "Object Inspector" open > Network tab > "Disable cache". The above directive cannot result in a 503. If you are getting a 503 there is likely to be a conflict with other directives. I notice you still have an HTTPS redirect? The above should replace all your redirects. I'll update my answer.
    – MrWhite
    Oct 8, 2015 at 9:41
  • Sure. Also, are you sure that setting wordpress url and site url to www.yourtechchick.com is not the conflict here? Oct 8, 2015 at 9:43
  • Sorry, there was a silly mistake in my code... Should have been RewriteRule not RewriteCond - corrected.
    – MrWhite
    Oct 8, 2015 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.