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
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 ****
public_html/domain.com/
, where /domain.com/
is the name of the hosted domain name(s)Example:
domain.com
awesome
awesome.apps.domain.com
will point to...public_html/domain.com/apps/awesome/
ubertz.apps.domain.com
will point to...public_html/domain.com/apps/ubertz/
I'm using this code for subdomain forwarding:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.apps\.domain\.com$
RewriteRule (.*) /apps/%1/$1
Explanation:
RewriteCond
you catch app name using regular expression (.*)
- it will be saved to variable %1
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.
The first thing that you need to do is to create a wildcard *.apps.domain.com sub-subdomain.
(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.
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".
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With