I have heard this thread (.htaccess redirect all pages to new domain) very informative. However, my request is a bit different from this original request.
Above thread talks about redirecting from old domain to a new domain, however, I have requested to only forward main domain to a subdomain.
Here is a question - How do I automatically forward all the request from my main domain to subdomain. For example -
Landing URL: http://example.com
Forwarding URL: http://sub.example.com
Landing URL: http://example.com?var=foo
Forwarding URL: http://sub.example.com?var=foo
If someone lands on my forwarding URL, I do not want them to be again redirected with 301. In simple words, I do not want to use a rule which always redirects all the request which lands on http://sub.example.com if the landing URL is the same.
If I follow the advice from the above link, I believe that will happen in my case as I want to forward from the main domain to a subdomain.
Here is what I have so far and it is doing partial job. It only forwards my one domain to another subdomain. However, it does not work when there are any URL parameters, it just stays the same old URL.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^$ http://sub.example.com/? [R=301,NE,NC,L]
In simple words - my code does not react when there is http://example.com?var=foo and it remains as it is.
Any guidance would be great.
You need to remove a condition and change your regex.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)/?$ http://sub.example.com/$1 [R=301,NE,NC,L]
Clear your browser cache then give that a try. Let me know how it works.
Related
So I'm trying to redirect traffic that goes to other pages using htaccess from a domain to a new subdomain but I'd like to keep traffic to the homepage there.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/?$ "http\:\/\/newsubdomain\.domain\.com\/" [R=301,L]
This just redirects everything to the new subdomain. That's where I'd like some help, how can I keep the traffic that comes to the homepage on olddmain.com and redirect queries to olddomain/post and have them go to newsubdomain.domain.com/post
Thanks
This just redirects everything to the new subdomain.
No it doesn't. (If everything is being redirected then either something else is doing that or you are seeing a cached redirect.)
The directives as posted actually do the exact opposite of what you're trying to do. These directives only redirect the homepage (ie. requests for the document root).
To redirect everything, except the homepage, you can do something like the following at the top of the .htaccess file:
# Redirect everything except the homepage
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule !^$ https://newsubdomain.domain.com%{REQUEST_URI} [R=301,L]
The RewriteRule pattern !^$ matches everything except the root (an empty URL-path).
You will need to clear your browser cache before testing.
Test first with 302 (temporary) redirects to avoid potential caching issues.
Additional notes:
There is no need to backslash-escape colons, slashes and dots in the RewriteRule substitution string (2nd argument) - this is an "ordinary" string, not a regex. (This unnecessary escaping is quite typical of an attempt at using cPanel's "redirect" generation.)
I assume this should be HTTPS, not HTTP?
The two conditions (RewriteCond directives) can be combined into one.
Let me explain my setup here, I have two domains, for the sake of naming them lets call them domain1.com and domain2.com. I have shared hosting that runs cPanel.
Domain1.com is my main domain, and is what I have my hosting account setup using. I have a website hosted at that domain. Domain2.com is set as an add-on domain, and directs to a folder inside of the main domains root. That has it's own website (a Ghost blog).
That all works fine. However the websites hosted at the two domains are quite different and I did not want the subdomain to work, but as far as I can tell add-on domains in cPanel have to have a subdomain in order to be added to the account. The redirect options for the subdomains in cPanel aren't good enough for me, as I wanted anyone accessing say domain1.com/domain2 to get a 404 error as if it didn't exist. So I set up the .htaccess file for domain1.com to look like this:
RewriteEngine On
#301 (permenant) redirects all HTTP requests to HTTPS (SSL)
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#404 redirects all domain.com/subdomain requests
RewriteCond %{HTTP_HOST} ^(www.)?domain1.com$ [NC]
RewriteCond %{REQUEST_URI} ^/domain2/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
#404 redirects all subdomain.domain.com & www.subdomain.domain.com requests
RewriteCond %{HTTP_HOST} ^domain2.domain1.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain2.domain1.co.uk$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ - [L,R=404]
This worked perfectly, anyone tryting to access domain2 via domain1 would get a 404 error. But that was before I installed Ghost on domain2 After getting Ghost installed I created an .htaccess file in the domain2 folder inside of domain1's root.I had to put this in it in order for Ghost for to run, as it uses NodeJS:
RewriteEngine on
RewriteRule ^(.*)$ http://127.0.0.1:55555555/$1 [P,L]
This also works perfectly except for one aspect. Now all requests to the domain using domain1.com have started working again. For example if I type domain2.domain1.com it will now show me the Ghost blog where as before it was correctly displaying a 404 Not Found error. Why is this and how do I go about rectifying this issue?
As a side note, I also started trying to have all http requests redirect to https. I added the following to the domain1.com htaccess file as I wanted all requests on both domains to redirect to https and I assumed this was necessary:
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Now this kind of works. All requests, whatever they are (with or without www for instance), redirect to https, and when I visit it in my browser I get a green the icon indicating it's secured with SSL. Again even with this the previous issue of the domain redirect still doesn't work. But I have another odd issue here.
If I type in my browser simply "domain1.com" (ie without https:// or www. or a combination of the two), it will show just "domain1.com" in the address bar but with the green icon and say it is secured. If however I type in "domain2.com" (again with no https:// or www etc.) it does the same thing except it states it is not secure, suggesting it requested http.
And yet if I type in www.domain2.com or http://domain2.com, that works and I get the secured icon. It is highly puzzling. It seems that the https redirect works on every request except for just "domain2.com" while any other variation (www.domain2.com, http://domain2.com etc) works fine. Any clues? I mean clearly I've done something wrong with the htaccess files but I don't know what, I got most of what I'd put in them from searching sites like Stackexchange, but personally I don't really know anything about them or how they work.
I think I have fixed my own problem here.
I corrected the main htaccess so the https redirect acts like this;
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
From what I can tell it's better to do it this way than the way I had it previously. This in itself didn't fix the problems. What fixed it for me is editing the domain2.com htaccess to look like this:
RewriteEngine on
#Force WWW if it isn't in the request
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
#Or include WWW even if it is in the request
RewriteCond %{HTTP_HOST} ^www\. [NC]
#Then Force it to use https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Defines this domain so main htaccess rules for subdomains work
RewriteCond %{HTTP_HOST} ^(.*)?domain2.com$ [NC]
#Proxy for Ghost
RewriteRule ^(.*)$ http://127.0.0.1:55555555/$1 [P,L]
Now everything works. All requests are redirected to https. All requests to the subdomain from the main domain fail with a 404. domain2.com now redirects to https://www.domain2.com or https://domain2.com so it is now always secured by SSL. I'm a novice at htaccess as is probably evident and I've no doubt some or all of this could have unnecessary elements removed or simplified so if anyone wants to correct my syntax so it's less messy I'd appreciate it.
What I'm trying to do is as follows:
Redirect user to another address using htaccess.
Pass the given URL as a parameter to the new url
So when the user visits any page on http://domainA.com/
he should be redirected to: http://domainB.com/?referer=http://domainA.com/
What I keep failing on, is retrieving the full URL the user came from BEFORE the redirection.
One of the things I tried and failed miserably:
RewriteRule ^(.*)$ http://domainB.com/?referer=%{HTTP_HOST}%{REQUEST_URI}
Thanks in advance!
Edit:
I tried to keep my question simple, as more complex questions tend to remain unanswered,
So I will sharpen my question, although there's a good answer to the original one.
So to be more specific about what I'm trying to achieve:
Both domains are hosted on the same host, and points to exact same files.
So domainA.com/file1.html and domainB.com/file1.html will display the same file.
What I want domainA to do, is to deliver all requests to a file called listener.php.
So that all requests to domainA should be like so:
User enters http://domainA.com/file1.html
Server request behind the scenes is actually: /listener.php?actualRequest=http://domainA.com/file1.html
I want this functionality to be on the server side so that the url will remain normal.
I went for 2 domains as I wanted to avoid redirect loops.
You can use this rule:
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainA\.com$ [NC]
RewriteRule ^ http://domainB.com/?referer=http%1://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
This handles both http:// and https:// URLs.
EDIT: As per edited question you can use:
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainA\.com$ [NC]
RewriteRule !^listener\.php$ /listener.php?actualRequest=http%1://%{HTTP_HOST}%{REQUEST_URI} [L,QSA]
I am using a single domain certificate (only on the www. subdomain) and want to have all users using this.
Currently i have the following in my htaccess file:
## force HTTPS and www. if any of them are not already present
RewriteCond %{HTTP_HOST} (?!^www\.)^(.+)$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
But the problem is, that this seems to lead to two separate redirects:
http://domain.com -> https://domain.com -> https://www.domain.com
This means 3 RTTs for the user until he reaches the correct page. In addition only the second redirect seems to be permanent, the first one (http to https) seems to be temporary,
Is there any solution which directly sends requests of from http to https+www?
Many thanks in advance!
There's only one redirect. The target is https://www.%1. There is no redirect to http://www.%1 and no redirect to https://%1. Only https://www.%1.
No idea how you arrived at the conclusion that this rule caused 2 different redirects. Is this what your access logs are saying? Or the browser's network console?
There is an external website which is redirecting all its traffic to webpages on our site. We can't control this redirection and the content URLs are mirrored in the following way.
When users go to:
www.externaldomain.com/1234
they get redirected to:
www.ourdomain.com/1234
which can sometimes be a non-existant page and thus a 404 error results. How can I re-direct all traffic from externaldomain.com to ourdomain.com homepage to make sure we don't lose potential users?
I know HTTP_REFERER comes into play, but I'm having trouble setting it up getting it to function properly.
See this answer
You must use modrewrite of apache:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^mydomain.co.uk$ [N]
The most important question to ask here is whether you wish to redirect all traffic to your homepage or specific pages that you've mentioned 'sometimes' don't exist (404) - both of which which are answered here.
Redirecting all referred traffic to a homepage from a specific external domain
Where you need to redirect all traffic from a specific domain (referrer), you can do the following:
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://externaldomain\.com/ [NC]
RewriteRule ^(.*) /index.php [R=301,L]
Redirecting only 404 error traffic to a homepage from a specific domain
If you want to redirect on this basis then use:
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://externaldomain\.com/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php [R=301,L]
With regard to the apache flags noted above:
[NC] = No case (or case insensitive)
[L] = If the rule matches, no further rules will be processed.
[R=301] = Apply a 301 redirect*
You can find more verbose/detailed info on Apache flags here.
*Search engine optimisation for these rules
Why a 301 redirect? - A 301 redirect is a permanent redirect which passes more than 90+% of link juice (or potential ranking power) to the redirected page. 301 refers to the HTTP status code for this type of redirect. In most instances, the 301 redirect is the best method for implementing redirects on a website in terms of SEO. In terms of benefit, there is a wealth of information here on how to capitalise on and deal with different types of redirect.