HTACCESS: How does a URL rewrite a subdomain?

I have a subdomain apps.domain.com pointing to domain.com/apps

I would like to rewrite the URL appName.apps.domain.com , where appName is a variable pointing to domain.com/apps/appName

  • I am looking for an internal address (without changing the browser url)

What do I need in my .htaccess file and where to save it? In the folder /apps/ ? Or in the root folder?

Finally, if this is not possible with the .htaccess file, how can I do this? I am using Godaddy Linux server as the host.


**** Question updated 07/08/2018; more details added ****

  • I am using GoDaddy SHARED hosting
  • Currently hosting multiple domains
  • Thus, the directory structure is public_html/domain.com/ , where /domain.com/ is the name of the hosted domain name (s)
  • Subdomain is exclusive to one specific domain

Example:

  • If the domain name is domain.com
  • There are several names, but for an example. if the application name is awesome
  • If uri: awesome.apps.domain.com will point to ... public_html/domain.com/apps/awesome/
  • If uri: ubertz.apps.domain.com will point to ... public_html/domain.com/apps/ubertz/
  • And so on...
+7
url-rewriting .htaccess
source share
3 answers

Create subcategory sub-map

The first thing you need to do is create a subdomain of the * .apps.domain.com subdirectory.

  • (If you are using Nameservers, skip this step!) Log in to your domain name registrar and create an A record for * .apps.domain.com (yes, an asterisk) and point it to the server IP address. Please note that distribution may take up to 48 hours.

  • Log in to your web hosting account and go to the "Subdomains" menu in the Domains section. Create a Subdomain * .apps.domain.com that points to the /public_html/domain.com/apps folder as the Root Document . And wait until the spread is over.

Visit How to create a substitution subdomain in cPanel and How to create a Sub-Subdomain for more information. If your server configuration did not allow you to create a substitution subdomain, simply create * .domain.com and point it to "/public_html/domain.com/apps".

Rewrite wildcard subdomain

Place these directives in the .htaccess file of the document root from the * .apps.domain.com wildcard, which in this case is "/public_html/domain.com/apps".

 RewriteEngine on RewriteCond %{HTTP_HOST} ^(.+)\.apps\.domain\.com RewriteRule (.*) http://domain.com/apps/%1/$1 [QSA,P,L] 

%1 is a numbered trackback (.+) , And $1 is (.*) . You must enable the [P] flag if the URL is rewritten from a specific domain http: // domain.com/apps/%1/$1 on the same server, without which it will be redirected. Use the [QSA] flag if you intend to use the query string of the rewritten wildcard subdomain. Visit P | proxy and QSA | qsappend for more information about them. And just adjust some setting, if I forget something, besides this, this is a server error.

A bit to think about

You must configure your .htaccess file for duplicate subdomains that will exist, such as otherwildcard.apps.domain.com, another.wilcard.apps.domain.com and other.deep.level.apps. domain.com, which will duplicate apps.domain.com, since they all have the same document root. Set up redirection or tell the client that these subdomains do not exist.

+4
source share

I use this code to forward the subdomain:

 RewriteEngine on RewriteCond %{HTTP_HOST} ^(.*)\.apps\.domain\.com$ RewriteRule (.*) /apps/%1/$1 

Explanation:

  • In RewriteCond you catch the name of the application using a regular expression (.*) - it will be stored in the variable %1
  • In RewriteRule you send everything - another expression (.*) , The contents of which will be saved in the variable $1 - to the directory /apps/<appName>/<path after URL>

For more information on regular expressions, I recommend checking out this tutorial: http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.php

You must save .htaccess in the root directory of the domain, in your case, in public_html/domain.com/ . If it does not work, put it in the apps folder.

+3
source share

Try these .htaccess directives:

 RewriteEngine on RewriteCond %{REQUEST_URI} ^(.*)\.apps\.domain\.com$ RewriteRule (.*) /apps/appName/$1 [P,L] 

RewriteRule does not redirect the user due to the [P] flag. This will redirect the request to the mod_proxy module, which processes the request and returns the result.

See http://httpd.apache.org/docs/2.4/mod/mod_proxy.html for more details.

+2
source share

All Articles