2

I want to redirect from a subdomain to domain along with URL path forwarding.

Example:

abc.example.com/products.html 

TO

another.example/products.html

The trailing URL should be preserved.

We have more than 1000 subdomains created and some of them needs to be redirected to Domains. The subdomains are created virtually.

I am using Linux webserver and PHP as language.

I believe this can be done by changing IP of another.example to example.com and adding some codes in the .htaccess file.

  • Do you have access to the server config? Are you always redirecting to another.example - or could the target domain vary? Do your subdomains point to a common root directory at example.com? – MrWhite Nov 11 at 12:39
  • The target domains vary. The subdomains are wildcard virtual subdomains. Do I have to create a Cpanel account for each of the target domains as well? – Amit Nov 11 at 15:35
  • "Do I have to create a Cpanel account for each of the target domains as well?" - You don't have to create a "cPanel account", but the target domain must obviously resolve and the redirected resource must be available at the target domain. – MrWhite Nov 11 at 16:01
1

I believe this can be done by changing IP of another.example to example.com and adding some codes in the .htaccess file.

You certainly should not be "changing the IP" of any domain. (What was your reasoning behind this?)

This sounds like a relatively simply redirect that can be performed in the .htaccess file. A condition is required that checks for the specific subdomain.

I assume all your "virtual" subdomains of example.com point to the document root of example.com. In which case you would put something like the following (using mod_rewrite) near the top of the .htaccess file in the root of example.com:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^abc\.example\.com [NC]
RewriteRule ^ https://another.example%{REQUEST_URI} [R=302,L]

The RewriteCond (condition) checks for the specific subdomain/hostname (note that this is a regex). The REQUEST_URI server variable contains the requested URL-path, which is appended onto the end of the target domain.

Note that this is a temporary (302) redirect. Only change this to a permanent (301) redirect - if that is the intention - once you have tested this is working OK. 301s are cached persistently by the browser so can make testing problematic.

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.