Redirect based on port number? - .htaccess

Is it possible to redirect domain.com:88 to domain.com:8080 using htaccess & mod_rewrite?
And example please?
If htaccess can't do it, waht are other options?

If you get requests on different ports you need to have servers listening on those ports. Inside those servers you can certainly configure rewriting rules, you don't even have to check the port, since it is implicitly given inside the servers logic.
Note that you can configure several virtual servers inside an apache http server.
You can also configure a single server to listen on several ports. Check for documentation about the Listen configuration option inside the http server configuration. Then indeed you have to test for the port. You can do that inside the rewriting module by using the RewriteCond command together with the SERVER_PORT variable. So something like:
RewriteEngine on
RewriteCond ${SERVER_PORT} 88
RewriteRule ^(.*)$ http://some.server.addr:8080/$1 [QSA,L]

Related

redirect domain to sub directory using .htaccess

My server has following directory in the web directory
/mydomain/site/
/mydomain/site/project1/
/mydomain/site/project2/
I want to point domain http://mydoman.com to site directory /mydomain/site/ and access project directories using http://mydoman.com/project1/ and http://mydoman.com/project2/
I tried following code. When I type http://mydoman.com/project1/ in the browser, it is working fine but the problem is when i type http://mydoman.com/project1 (without "/" in the end of url) the url changes to http://mydoman.com/mydomain/site/project1/
RewriteEngine On
RewriteCond %{HTTP_HOST} mydomain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/site/.*$
RewriteRule ^(.*)$ /mydomain/site/$1 [L]
what I need is when I type http://mydoman.com/project1 url should not change to http://mydoman.com/mydomain/site/project1/
also
this url should not work http://mydoman.com/mydomain/site/project1/
Not sure why you don't want to use separate host names for separate projects. That would save a lot of hassle. Like https://example.com/... and https://project1.example.com/....
But anyway, this probablyis what you are looking for:
RewriteEngine on
RewriteRule ^/?mydomain/site/(.*)$ /$1 [R=301,QSA]
RewriteRule ^/?site/(.*)$ /$1 [R=301,QSA]
RewriteRule ^/?(.*)$ /mydomain/site/$1 [END]
You also need to take care that your application logic uses clean, relatvie references and not absolute paths like /site/... or /mydomain/site/... or even full URLs like https://example.com/mydomain/site/.... But that has nothing to do with rewriting. You need to solve that directly in your application logic.
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Redirect addon domain to webapge htaccess

Not quite sure if this is possible. So I have domain2.com and it maps to domain.com.
On the site I have a page called /domain-2-landing
When I hit domain2.com I want that to redirect to domain2.com/domain-2-landing or even better yet mask domain.com and serve the content of /domain-2-landing
Is this possible via .htaccess?
Your question is a bit vague, since you write about two separate things. Here are two approaches that hopefully will point you into the right direction:
To redirect any request to "domain2.com" to that "landing page" this probably is what you are looking for:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain2\.com$
RewriteCond %{REQUEST_URI} !^/domain-2-landing$
RewriteRule ^ /domain-2-landing [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
To deliver the content of that page as a response to requests to "domain.com" there are two diffferent situations:
If the domains are served from separate http servers you can use the proxy feature integrated into the rewriting module if the proxy module is installed:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^ https://domain2.com/domain-2-landing [P,END]
If both domains are served from a single http server you can do something similar as above, if both hosts share the same DOCUMENT_ROOT:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^ /domain-2-landing [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
These rules will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Append internal traffic with ?internal=true based on IP address

I am trying to append any url with ?internal=true if the IP address is one of mine (where x.x.x.x = the IP)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^x.x.x.x$
RewriteRule ^(.*)$ ?internal=true [L,QSA]
</IfModule>
Your approach is nearly perfect, I just made a tiny modification:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^x\.x\.x\.x$
RewriteRule ^/?(.*)$ /$1?internal=true [END,QSA]
The specific issue with your exact code is that you omit the actual path component of the requested URL when rewriting. Instead you need to preserve it using the $1 reference.
The "x.x.x.x" obviously is meant as a placeholder for an IP address.
When matching for IP addresses keep in mind that the quadrupel notation you use only matches IPV4 addresses. Many networks started to use the advanced IPV6 in the mean time. You will have to adjust your code once your own network switches.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

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]

Pointing my domain to my node.js instance

I own a server running whm / cpanel, and I have recently been experimenting with node.js. I have a little node test server running and i would like to point a domain at it. Up till now, I have been using whm to create accounts and deal with domains, but this is a different scenario, and I have little knowledge about how to make a domain point to something on my server when there are multiple other domains pointing to other different things on my server at the same time.
Thanks
Ok, I have never tried this, but you could use a rewrite rule in a .htaccess file and rewrite everything to goto to port 8000 where you have your node.js server running.
So first, set up your node.js server to listen to port 8000.
Then, create your domain like normal in cpanel, and in the doc root add a .htaccess file with this rewrite:
RewriteRule ^ "\ http: \ / \ / 127.0.0.1 \:% (8000) REQUEST_URI" [P, QSA, L]
That should just forward everything internally to port 8000 where node.js will take care of it. But I don't know how persistent connections / websockets will work with this strategy. It may be better to skip apache in those cases by going directly to the port 8000 on your server.
Source: http://es.w3support.net/index.php?db=sf&id=59994
This is a little bit tricky, since cpanel is going to use apache to configure the domains, and apache has already taken port 80 without a doubt, so you can't share port 80 between apache and node.js. You will not be able to configure this through cpanel.
You could just point the domain to your server and have node listen to another port such as 8000.
Then go to http://mydomain.com:8000/
So apache/cpanel handles requests to ports 80 and 443, and node handles requests to port 8000.
It is very possible to run multiple web servers on one box, but they each need their own port(s)
I managed to do it like that:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_PORT} !^3000$
RewriteRule ^ http://%{HTTP_HOST}:3000%{REQUEST_URI} [P,QSA,L]
</IfModule>
Anything else didn't worked for me :(
Here I check if the port is not 3000 and if the protocol is not https then I point the domain to port 3000 (you can put the port you use with node.js) you can remove the RewriteCond %{HTTPS} !=on in case you don't care to check for the protocol.
I do some R&D locally but in production I believe I will use nginx + node instead of apache.

Resources