Map domain name to a space on the server - forward

I have a domain, e.g., domain.org and a hosting space on the server that has the address http://server/node/myname.
How I can map domain.org to http://server/node/myname such that when I visit to domain.org/some/path I go to http://server/node/myname/some/path?
The domain service offers me web forwarding with HTTP/301 Forwarding or Cloaking Forwarding but none of them works for me. They simply redirect me to http://server/node/myname when visiting domain.org/some/path.

If you have access to apache config It would be Directory config within your VirtualHost config part. see https://wiki.ubuntuusers.de/Apache/Virtual_Hosts/
You can write redirects within your site like here:
https://en.wikipedia.org/wiki/URL_redirection

Modify your .conf file if you have access to Apache configuration like this...
RewriteEngine on
RewriteRule ^(.*) http://server/node/myname/$1 [P]
This will redirect all traffic for the base URL you set above this in that configuration file.
If you already have a rewrite rule in place for domain.org you can use scripting within those pages to do simple URL redirects by parsing the URL on domain.org into a new string for http://server/node/.

Related

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...
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.
Create wildcard sub-subdomain
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".
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.
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.

Redirecting web content but not the name of web page address

I have two internet addressess, say one.com and two.com
The content of my webpage is all under the domain one.com. If I hit address two.com I want it to redirect to one.com but still with address two.com. For example if I type in address bar two.com/article. I want to still show this same address but the content displayed would be as from address one.com/article
I tried to use htaccess file, but still no luck.
Any advice would be appreciated.
You can't achieve that while performing an external redirect.
If both domains are hosted on the same server, then you can perform an internal redirect (e.g. using Apache's Alias, AliasMatch or mod_rewrite).
If the domains are hosted on different servers, then you would have to proxy one of them. You could do this with ProxyPass from Apache's mod_proxy.
Go to your domain name provider and create an alias (CNAME record) for the address.
You will need to enable mod_proxy in your Apache config for that. Once mod_proxy is enabled, enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory on domain2.com host:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$ [NC]
RewriteRule ^ http://www.domain1.com%{REQUEST_URI} [L,P,NC]

Masking sub domain with a new domain while preserving the paths

I own a domain since long, just masking the names:
http://mydomain.com
Later I started using a subdomain on this domain for some project.
http://subdomain.mydomain.com
Those projects grew and now I have a structure like
http://subdomain.mydomain.com/project1
http://subdomain.mydomain.com/project2
http://subdomain.mydomain.com/project3/subproject1
http://subdomain.mydomain.com/project3/subproject2
http://subdomain.mydomain.com/project3/subproject3
http://subdomain.mydomain.com/project4
....
etc.
now I bought a new domain (shortdomain.com) where I plan not to move anything but everything should be accessible via redirects so everything looks like:
http://shortdomain.com
http://shortdomain.com/project1
http://shortdomain.com/project2
http://shortdomain.com/project3/subproject1
http://shortdomain.com/project3/subproject2
http://shortdomain.com/project3/subproject3
http://shortdomain.com/project4
...
etc.
So basically I need to do two things:
1. if anyone visits my old domain, redirect them the new naming structure. i.e. if someone loads http://subdomain.mydomain.com/project2 they should be redirected to http://shortdomain.com/project2
when a user loads/redirected to http://shortdomain.com/project2 this should actually load the content present at http://subdomain.mydomain.com/project2
So I will not manually migrate projects,codes and GBs of other data. I think this might be acievable by smart redirection only.
Just FYI:
1. I have full DNS control of both the domains
2. I am hosted on hostgator
3. I use cloudflare on the first domain and would like to continue using it
I think this might be acievable by smart redirection only.
No, redirection changes what's in the browser's location bar. If you redirect to shortdomain.com then the request will get sent to shortdomain.com, and have nothing to do with subdomain.mydomain.com anymore. If you redirect back to subdomain.mydomain.com, then the location bar in the browser will change as well.
What you really want to do is point shortdomain.com to the same server and document root that subdomain.mydomain.com is on. Then use this to redirect (either in htaccess file or server config):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://shortdomain.com/$1 [L,R=301]
If, for whatever absurd reason you can't point the shortdomain.com DNS to the same webserver that serves subdomain.mydomain.com, or can't setup that webserver to accept requests for the shortdomain.com host, you need to setup a proxy server. And it'll work something like this:
2 Webservers, server A (hosts subdomain.domain.com) and server B (hosts shortdomain.com)
Someone requests http://subdomain.mydomain.com/project3/subproject1
server A gets the request and redirects the browser to http://shortdomain.com/project3/subproject1
browser's location bar changes to new location
server B gets the request and reverse proxies the request back to server A
server A gets the request again but must recognize that it is a proxy and then serve the page instead of redirecting
As you can see, this is a horrendously ineffecient solution. It's also a high possibility that your hosting service won't allow you to setup proxy servers.
I have full DNS control of both the domains
With full control I assume you can enable mod_proxy as well on Apache web-server of shortdomain.com. Once that is done set it all up this way.
On subdomain.mydomain.com enable mod_rewrite and place this rule in Apache config OR DOCUMENT_ROOT/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
RewriteRule ^ http://shortdomain.com%{REQUEST_URI} [L,R=301]
On shortdomain.com enable mod_proxy, mod_rewrite and place this rule in Apache config OR DOCUMENT_ROOT/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^shortdomain\.com$ [NC]
RewriteRule ^ http://subdomain.mydomain.com%{REQUEST_URI} [L,P]

Linking one domain to another domain with htaccess

I'm stuck with htaccess redirect on this case:
I have myapp.com domain where my main website and service runs on. My customers logs into their accounts on myapp.com and use. Now, I am going to provide one of the features on a separate domain, let's assume "goto.com". However, I don't want to build a separate app on goto.com. I just want to redirect all coming requests to goto.com to a php script under myapp.com but this redirection should be in the backend (masked), not a 301 redirection.
Let me clear up:
myapp.com - /var/www/vhosts/myapp.com/httpdocs/index.php
goto.com --> masked redirection --> myapp.com/goto.php?$1
How can I do this with htaccess? Any suggestions?
Just found that it can be done with redirect [P] (proxy) method: RewriteRule ^(.*)$ http://myapp.com/public_view/$1 [P] What do you think? Is this a good and stable method?
That method is fine, it utilizes the mod_proxy module. The only thing I can suggest is adding the L flag as well in case you have other rules in your htaccess file.
A limitation with using the P flag in an htaccess file is that if a page or request to http://myapp.com/ redirects, then you're URL address bar will say http://myapp.com/ instead of your other domain. The only way around this is to use ProxyPassReverse but it doesn't work in an htaccess file. You'd need access to vhost config:
ProxyPass / http://myapp.com/public_view/
ProxyPassReverse / http://myapp.com/public_view/
Additionally, if http://myapp.com/ sets cookies, you'll need to use the ProxyPassReverseCookieDomain and ProxyPassReverseCookiePath directives to rewrite those.

how to turn off .HTACCESS rewrite engine off for a file

i have rewrite engine on with a rule to attach www in every url being accessed on my website.
the rule is written in htaccess located in public_html folder. now a problem is i have registered a web application in windows live developer website. as you may know, the windows like applications doesn't accept/allow domains to be verified as www.mydomain.com. they only accept mydomain.com format. so the return url specified in application settings is not delivering data as my verified domain is "mydomain.com" and in return url "www" is being attached due to htaccess rule. so i would like to know if there is any way i can exclude my 1 specific file (not the folder) from all rewrite rules being applied. or is there any way i can verify my domain as www.mydomain.com in windows live applications?
also, is there any way to turn off rewrite rules off for that particular file from PHP?
In your .htaccess rule you can add a condition like this:
RewriteCond %{SCRIPT_FILENAME} !.*myFile.asp$ [NC]
to execute your rewrite rules for everything EXCEPT a file called myFile.asp.

Resources