2

I am redirecting example.com to sample.com/?example, so that I can have a modal popup saying, example has changed its name to sample.

It works fine for HTTP redirects from http://example.com, and almost everything works for HTTPS redirects from https://example.com, except for these two things (applying only to the redirects from https://example.com):

  1. In the address bar, it will still say https://example.com, but show the page for https://sample.com. Whenever a link is clicked on this page, the problem is fixed and it then shows https://sample.com in the address bar.

  2. It will not keep the query that I add (?example=1).

Here are my .htaccess rules:

RewriteCond %{HTTPS} ^example.com$ [OR]
RewriteCond %{HTTPS} ^www.example.com$
RewriteRule (.*)$ https://www.sample.com/$1?example=1 [R=301,L]

How can I fix these so that when I go to https://example.com, it goes to https://sample.com?example=1

  • The only things I see are that for your RewriteCond you are not escaping the . [dot] in your URLs so that it would be ^example\.com$ and not using NC for no case in the [] at the end of the line. Your first line would be [OR,NC] (no space) and your second line would be [NC]. Otherwise, I am not seeing anything else right now. – closetnoc Feb 4 '15 at 2:01
  • You say the problem is with HTTPS redirection, but the code you have posted does not redirect to HTTPS - it redirects everything to HTTP!? That certainly is a problem, but a different one to what you mention. Do you have some more code? (The NC flag would be unnecessary, unless you specifically want to allow strange requests.) (I've removed the space from the _substitution URL - I assume that is not intentional, as per your comment) – MrWhite Feb 4 '15 at 8:30
  • I corrected it with the HTTPS redirect instead of the HTTP redirect and whenever this (except with the correct URL name) is in my .htaccess, it still doesn't work as I described above. – lawdawg Feb 4 '15 at 17:40
  • Is this the actual code from your .htaccess file? As it stands, the code above won't actually do anything. %{HTTPS} contains either "on" or "off", so the RewriteCond rules will always fail. Are you redirecting HTTP to HTTP and HTTPS to HTTPS, or everything to HTTPS? – MrWhite Feb 4 '15 at 18:53
  • Yes it is. I am trying to redirect HTTP to HTTP and HTTPS to HTTPS – lawdawg Feb 4 '15 at 19:12
2

You must have more directives in your .htaccess in order to do the complete HTTP and HTTPS redirection. So, filling in the gaps...

For the sake of simplicity I've assumed you don't have any other subdomains (apart from www). So you are redirecting everything at example.com to www.sample.com.

# Redirect http://example to http://sample
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} example\.com
RewriteRule (.*) http://www.sample.com/$1?example=1 [R=301,L]

# Redirect https://example to https://sample
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} example\.com
RewriteRule (.*) https://www.sample.com/$1?example=1 [R=301,L]

%{HTTPS} is always set (regardless of whether SSL is enabled or not) and simply contains the value "on" or "off".

The trailing $ (end of pattern marker) on the RewriteRule pattern is not required. And the 2nd argument to RewriteCond (the CondPattern) is a regular expression (most of the time) so the dots should really be escaped, otherwise they match anything.

  • Sorry for the late response - it works for every page, but the home page, which is weird, but does well enough. I still get the problem listed above on type "example.com" Thanks! – lawdawg Feb 7 '15 at 19:53
  • In that case there would seem to be some other code that is affecting this. The above directives would need to go near the top of your .htaccess file (order matters). Are you saying that both #1 and #2 are still a problem? Are both example.com and sample.com on the same server? Are they both on the same account (occupying the same hosting space)? – MrWhite Feb 7 '15 at 23:40
  • Sorry for the late response, they are both on the same shared hosting account. I removed all the other redirect rules for example.com. Sample.com is the primary domain and example.com is an addon domain. – lawdawg Mar 8 '15 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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