Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTACCESS: How to URL rewrite a sub-subdomain?

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

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

  • I'm looking for an internal forward (no change in the browser's URL)

What should I have in my .htaccess file and where should I save it? Within /apps/ folder? Or within the root folder?

Lastly, if it is not possible with an .htaccess file, how can I do this? I'm using a Linux godaddy server as host.


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

  • I am using GoDaddy SHARED hosting
  • It is 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)
  • The sub-subdomain is exclusive to 1 specific domain

Example:

  • If the domain name is domain.com
  • There's multiple names, but to give an example. if the name of the app is awesome
  • If the uri is: awesome.apps.domain.com will point to...public_html/domain.com/apps/awesome/
  • If the uri is: ubertz.apps.domain.com will point to...public_html/domain.com/apps/ubertz/
  • And so on...
like image 390
Omar Avatar asked Jul 02 '15 13:07

Omar


3 Answers

I'm using this code for subdomain forwarding:

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

Explanation:

  • In RewriteCond you catch app name using regular expression (.*) - it will be saved to variable %1
  • In RewriteRule you forward everything - that's another (.*) expression which content will be saved in variable $1 - to the directory /apps/<appName>/<path after URL>

For more information about Regular Expressions I recommend to check this tutorial: http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.php

You should save your .htaccess in the root directory of the domain, in your case in the public_html/domain.com/. If it doesn't work, place it in the apps folder.

like image 164
Genhis Avatar answered Oct 04 '22 05:10

Genhis


Create wildcard sub-subdomain

The first thing that you need to do is to create a wildcard *.apps.domain.com sub-subdomain.

  1. (If you're using Nameservers, skip this step!) Log in to your domain name registrar, and create an A record for *.apps.domain.com (yeah, that's an asterisk) and point it to the server IP address. Note that DNS can take up to 48 hours to propagate.

  2. Log in your web hosting account and go to the menu 'Subdomains' under Domains section. Create a Subdomain *.apps.domain.com that's pointed to the "/public_html/domain.com/apps" folder as its Document Root. And wait until the propagation is over.

Visit How to create wildcard subdomain in cPanel and How to Create a Sub-Subdomain for more info. If your server's configuration didn't allow you to create a wildcard sub-subdomain then just create *.domain.com and point it to "/public_html/domain.com/apps".

Rewrite wildcard sub-subdomain

Place these directives in the .htaccess file of the document root of the wildcard *.apps.domain.com 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]

The %1 is the numbered backreference of (.+) and the $1 is of (.*). You need to include the [P] flag if you URL rewrite from a specific domain http://domain.com/apps/%1/$1 in the same server, without that it will be redirected. Use the [QSA] flag if you'll going to use the query string of a rewritten wildcard subdomain. Visit P|proxy and QSA|qsappend for more info about them. And just tweak some adjustment if I forgot something, other than that, is the server's fault.

Few things to consider

You must configure your .htaccess file for the duplicate subdomains that will be going to exist, such the otherwildcard.apps.domain.com, another.wilcard.apps.domain.com and the other.deep.level.apps.domain.com that will duplicate apps.domain.com as they're all have the same document root. Configure to redirect or to tell to the client that those subdomains aren't exist.

like image 34
5ervant - techintel.github.io Avatar answered Oct 04 '22 06:10

5ervant - techintel.github.io


Try these .htaccess directives:

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

The RewriteRule will not redirect the user because of the [P] flag. This will redirect the request to the mod_proxy module which handles the request and returns the result.

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

like image 22
davidgiga1993 Avatar answered Oct 04 '22 06:10

davidgiga1993