replacing domain names using .htaccess - apache

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/

Related

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.

Apache proxypass and multilingual domains

I have a website on domain.com which has multilingual content in URLs e.g.:
domain.com/en for english
domain.com/de for german
domain.com/it for italian
etc. These are not really directories - these are just rewrites that rewrite to /index.php?lang=... So domain.com/lang/one/two/three is rewritten to /index.php?lang=$1&path=$2
It also has domain.com/assets directory that holds all the common files (css, images etc.)
Now I want to change that every language has its own domain which I think could be managed by using mod_proxy, mod_proxy_http and mod_proxy_html in Apache. What I want to achieve is that if user visits:
www.domain.com he gets content from domain.com/en
www.domain.de he gets content from domain.com/de
www.domain.it he gets content from domain.com/it
And if he visits /assets directory from any domain (.com, .de, .it) he would get content from domain.com/assets. Also if user visits URL e.g. www.domain.de/something he should receive content from www.domain.com/de/something
And the other thing are URLs which would have to be rewritten before output so for instance user that browses domain.de would never go to domain.com at all...
Is this possible? Otherwise we would have to reprogram whole CMS to work with different domain for every language...
It's possible. Just handle them in a single virtualhost and use RewriteCond to examine %{HTTP_HOST} and do the same rewrites you're already doing today. You can capture the country code and backreference it with %1.

Apache: How can i redirect all sites on the server to an URL?

I have a VPS based Centos/cPanel-WHM. I wanna redirect all sites (including all pages & subdomains) on the server to one URL. How can i do this?
create .htaccess file in every website DocumentRoot dir which you want to redirect
# This allows you to redirect your entire website to any other domain
Redirect 301 / http://exampledomain-redirect.com/
At webserver layer (change the .htaccess), you could issue 301 redirects for any requests to your sites to new URL
OR
you could inject javascript (through your web layer) or your code layout framework OR manually
at the head of the page to complete a redirect.
OR
if your domains point to different hosting.. you could upate their NS to point to your new location and do 1 OR 2
.htaccess is the best way, Otherwise change the document root for each site.

Redirect all traffic from one folder to another

I have a blog hosted at say domain.com/site but have built a new website. I want to redirect all traffic from domain.com/site/anything to domain.com/home/ so that any traffic going to any page or file on the old site gets redirected to the new homepage.
I am unsure what code to place in the .htaccess file and also where on the server to place it
Simply:
Redirect 301 {old path} http://domain.com/{new path}
you can place that at the root of your website, with the old path and Apache will redirect to the new site.
In your case:
Redirect 301 /site/anything http://domain.com/home

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.