Changing the domain canonical name - apache

i have a number of domains that all point to www.domainA.com. now i need to change it so that all requests will point to www.domainB.com. notes: it is a virtual host, the machine also hosts other domains. and it wasn't me who setup the server on the first place.
thank you.

In your domainA's .conf
RedirectMatch 301 (.*) http://www.domainb.com$1
This will forward all requests coming in on domainA to the equivalent url on domain B, doing a 301 "moved permanently" status, so hopefully any crawlers following those links will update things to just point at domainB instead of going through domainA

Related

Will a htaccess rewrite rule to force www work without an A record in place for the apex/naked domain?

I'm working on a site that only works if you type in www. Only the www subdomain has an A record. The bare/naked domain is unable to be given an A record. The naked domain can also not be given a DNS Alias. This means the site without "www" shows an error in your browser.
I'm trying to think through any creative solution that may solve this usability issue.
Would an htaccess rewrite rule such as:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
work in this scenario to force users to the www working site and to keep users from seeing an error in their browser?
I'm thinking it likely does NOT work because the htaccess file would only be accessed when users already typed in www (which has the A record). Is that thinking correct?
(Additional examples of htaccess code are in this thread) Redirect non-www to www in .htaccess
If this isn't a valid option, any other creative solutions that might work?
No, .htaccess rules won't work if the DNS isn't pointing to them. The only way to issue a redirect is by pointing the DNS to a web server that redirects.
CNAME records never work for the domain apex, so they are never an option. The only DNS records that will resolve a website for the domain apex are A records. I suspect that you are using a web host that doesn't give you an IP address to put into the A record, only a name. They probably warn that the IP address could change without warning. There are other possible solutions in this type of situation:
Let your web host be your DNS host
Many web hosts will offer to host your DNS for you. They can then keep your A and CNAME records up to date for you. They would be able to use A records at the APEX and change them as needed. If your web host offers this service, you would change your NS records at your DNS registrar to point to values given to you by your web host. Usually something like ns1.example.com and ns2.example.com.
Use ANAME or ALIAS records
Many DNS hosts now allow your to enter a name but serve it as an A record. The DNS host periodically (like every few minutes) looks up the IP address for the name and serves an A record with the current value. These records have different names at different DNS hosts, but they are usually called ANAME or ALIAS records. If your current DNS host doesn't offer this service, you could switch to a more capable DNS host that does so.
Use a third party redirect service
If your existing web host won't give you an IP address to use in an A record, you can use an alternate web host that will do so for the redirects. Most web hosting will give you an IP address to put into DNS records that won't change without notice. It is possible to find an alternate host for your domain apex and use that hosting only to do the redirect to www. Your www CNAME record would continue to point to your existing host.

redirect any link on any subdomain-url to another domain

I registered a expired domain to forward all incoming links to another domain. The problem is: many inlinks are placed on subdomains, for example: axa-art.cdn.contento-v41.eu/axa-art/0eee9cec-58cb-45b2-a4e2-b5f73920068e_091216_axa+art_classic+car+study_de_rz.pdf
I am looking for a 301 redirect rule in htaccess that forward any url (no matter on main domain or subdomain) to "new-url.tld"
axa-art.cdn.contento-v41.eu
axa-art.cdn.contento-v41.eu/slug
any-subdomain.contento-v41.eu
any-subdomain.contento-v41.eu/slug
all of this example above should
forward to this exact URL: new-domain.tld
Question 1:
Is it possible to create a "general" rule and place it into htaccess of the main directory?
Question 2:
Or do i have to write a specific rule for each subdomain?
Question 3:
Do I have to create a sub-directory and create a separate htaccess in every sub-directory for each subdomain I want to add redirection-rules?
Help or suggestions are highly appreciated. Thank you very much for your help in advance.
This isn't just a .htaccess question. In order for your server to receive requests to <any-subdomain>.example.com the necessary DNS and server config directives need to be in place. If the request doesn't reach your server then you can't implement a redirect in .htaccess.
So, I suspect that these subdomains are not even resolving?
You either need to create the necessary DNS A records and ServerAlias directives one by one for each hostname (ie. subdomain) or create a "wildcard" DNS A record (and ServerAlias *.example.com directive in the vHost). But then you still have an issue with these hostnames being covered by an SSL cert if you need to redirect from HTTPS.
You can then create the necessary redirect in .htaccess. Although, since you need access to the server config (or a using a control panel that does this for you) to implement the directives above, you should also implement this redirect in the server config also.
For example, at the top of your .htaccess file, before the existing directives (or in your vHost):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^new\.example$
RewriteRule ^ https://new.example%{REQUEST_URI} [R=301,L]
The above states... for any request that is not for new.example then 301 redirect to https://new.example/<same-url>.
However, if you have access to the server config and this other domain is configured in its own vHost container then the redirect can be simplified:
Redirect 301 / https://new.example/
UPDATE#1:
this rule does forward any URL form the main domain to the new domain.
# Permanent URL redirect- by netgrade
RewriteEngine on
RewriteCond %{REQUEST_URI} !https://www.marco-mahling.de/$
RewriteRule $ https://www.marco-mahling.de/ [R=302,L]
The rule I posted above should probably replace your existing rule entirely.
Yes, your rule does redirect every URL to the root of the new domain, but it is arguably incorrect. The RewriteCond directive is superflous and isn't actually doing anything. The REQUEST_URI server variable contains the URL-path, it never contains the scheme + hostname. So, the RewriteCond directive you've posted will always be successful.
If that is the rule you currently have then it would already redirect everything. In which case your problem would seem to the necessary DNS and server config directives as mentioned above.
From your directives, I assume that the other domain actually points to a different server (or different vHost on the same server). Otherwise, this would have resulted in a redirect-loop. In which case, you only need the much simpler Redirect directive that I posted above.
UPDATE#2: That works fine BUT the incoming links are still not forwarded cuz of a "%" in the url: https://axa-art.cdn.contento-v41.eu/axa-art%2F0eee9cec-58cb-45b2-a4e2-b5f73920068e_091216_axa+art_classic+car+study_de_rz.pdf
It's actually because of the %2F - an encoded slash (/) in the URL-path. By default, Apache will reject such URLs with a 404 (for security reasons).
To allow encoded slashes in the URL you would need to set AllowEncodedSlashes On in the server config (or vHost container). You cannot set this in .htaccess. (The server generated 404 occurs before .htaccess is even processed.)
However, I would express caution about enabling this feature. (Is there a specific requirement here? Are you recreating these documents on the new server?)
If this request was intended to map directly to a PDF file on disk then this actually looks like an incorrectly URL encoded request, since a slash / is not a permitted filename character on either Windows or Linux.
If you enable AllowEncodedSlashes then the above RewriteRule will redirect the request to /axa-art/0eee9cec....pdf - note the %-decoded / in the resulting URL. You would need to take additional steps to maintain the URL-encoding (if that was required), but as I say, that looks like a mistake to begin with.

Drupal Domain Redirection

We have two domains: www.example.com and www.example.org. Both point to the same site. But, we'd like everything to point to .org. We are running Drupal on a LAMP server hosted by Media Temple.
We have now started to have problems because our .com SSL cert expired, so anybody who goes to the .com site routinely gets scary messages in Chrome, etc.
How can we make it so that if somebody goes to https://www.example.com in google, they get nicely directed to http://www.example.org?
You can do this multiple ways.
.htaccess
RewriteEngine On
Redirect 301 / https://www.exmaple.org
In a PHP file
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.example.org");
header("Connection: close");
You can try this module :
https://www.drupal.org/project/domain_301_redirect
This is the domain that all other domains that point to this site will be 301 redirected to. This value should also include the scheme (http or https)
I Hope help you!

replacing domain names using .htaccess

I have a new website as http://abc.com/case_studies/casstudy20/.
I have an old website as http://xyz.com/clients/home.php?client=myclient
There are loads of case studies and stories under old domain that is xyz.com. Now that I am using abc.com is it possible to replace the URL, so it shows abc.com but access the code from the same old place.
meaning when my users goes to old site instead of xyz.com. It should say http://abc.com/clients/home.php?client=myclient. But runs from the same old xyz.com.
Is it possible to do it using htaccess.
Sort of. You can redirect all the traffic from xyz.com to the proper path on abc.com but this requires you to maintain control of both domains.
If you can, it's actually better to put this in the virtual host config for xyz.com then you don't need a complete configuration at all. Assuming you have permissions it will work in .htaccess as well though.
RedirectMatch ^(.*)$ http://www.abc.com$1
If you can keep control of both domains then you are looking for a http 301 redirect. You can do that by adding the following line to your htaccess file to redirect an entire website. Without control of your original domain it's impossible for your old xyz.com website to be routed to the appropriate server so your htaccess file will never have anything to redirect.
redirect 301 / http://abc.com/

How to Redirect any subdomain request to main domain?

I'm trying to redirect all subdomain requests for domain.com to www.domain.com even when the subdomain does not exist, for example if we have:
abc.domain.com to www.domain.com
Where abc can be any requests. Furthermore, that subdomain abc may be exists or not. But whatever it is, I want to redirect it to main domain.
And less important request is. How it is possible to keep the input address at the address bar and redirect to main domain?
It will be best for me if it is done by .htaccess
I use apache server.
Thank you.
Since you haven't specified the environment you use, here are some general tips:
First, there is a difference between redirecting:
The user types sub.domain.com into the browser's address bar, but is redirected to domain.com -> domain.com is in the address bar, as the user is now on domain.com
...and rewriting an URL in the background:
The user types in sub.domain.com and stays at this address. The user sees sub.domain.com but in the background some other page (in your case, that one under domain.com) is loaded and shown.
(Quickly explained.)
If you are using Apache, take a look at 301 redirects and url rewriting.
In addition to what Piedone said (which is on the HTTP server side), you also need to configure the DNS to have a catch-all for all subdomains, directing them all to your HTTP server.
This implicitly means that all possible subdomains will exist automatically.