.htaccess Implement domain name without port access service - linux

You want to use the. Htaccess file to implement the service that accesses the specified port when there is' API 'in the path
The type is the same as nginx:
location /api/ {
proxy_pass http://localhost:8385;
}

In .htaccess you would need to use mod_rewrite in order to send the request through mod_proxy. The following is the equivalent of the Nginx config as posted:
RewriteEngine On
RewriteRule ^api/ http://localhost:8385%{REQUEST_URI} [P]
However, you will likely need access to the server config in order to ensure that mod_proxy (and associated modules are installed).
And, if you have access to the server config, then it would be preferable to do this in the main server config to begin with. For example:
ProxyPass /api/ http://localhost:8385/api/
ProxyPassReverse /api/ http://localhost:8385/api/

Related

apache and node express with node rewrites

I have the following problem:
I have an apache serving files under url.com/ and url.com/a
I also have a node-express server listening on port 3000, which is not publicly accessable.
Now, I would like to be able to access the node server for any url like url.com/b/.
My hosting company and google referred me to using .htaccess rewrites like so under url.com/, but it does not work:
RewriteEngine on
RewriteRule ^(.*)b(.*)$ https://url.com:3000/b/$1
Does the port need to be publicly accessable for the mod_rewrite approach to work?
What is the proper way to set something like this up?
Many thanks!
Try mod_proxy:
<Location /b>
ProxyPass http://localhost:3000/
ProxyPassReverse http://localhost:3000/
</Location>
unfortunately, since i deployed on a managed virtual server, i wasnt able to use Elvis' solution, modifying the httpd.conf file.
I ended up using this solution, modifying the .htaccess file in the folder public_html/b:
RewriteEngine on
RewriteRule ^(.*) http://localhost:3000/$1 [P]

Node.js and Apache for single domain same server

I built a RESTful website, and it is written in Angular2(front end) and Php(backend). Since I am using Angular Universal, I need Node.js to pre-process the Angular scripts. How can I configure my server to host both Node.js and Apache? I searched online, but can't find any resources related to "sharing Node.js and Apache on SINGLE domain"...
How to configure my Apache to route "everthing" to the /dist/ folder (and served by Node.js) except for the URI that are prefix with /api/ and /uploads/ (route to the /php/ folder).
[Back-end] Api Php script:
If www.example.com/api/?...?, then use this script /var/www/example.com/public_html/php/api.php to process it.
Uploaded images:
If asking for uploaded img files (www.example.com/uploads/[img*.png]), then try to look inside this /var/www/example.com/public_html/php/uploads/[img*.png] folder.
[Front-end] Html, Js, etc...:
Everyting www.example.com (except the /api/ and /uploads/), then ask Node.js to run it from this /var/www/example.com/public_html/dist/ folder.
I guess it is related to proxy and proxy_http, but I don't know how to configure it since I can't find any resources related to sharing both systems for single domain...
i think you should Run Node.js on a different port (e.g 8080) and use Reverse proxy in Apache config (virtual hosts) like this:
ProxyPass /api http://www.example.com/php/
ProxyPassReverse /api http://www.example.com/php/
ProxyPass /uploads http://www.example.com/php/uploads/
ProxyPassReverse /uploads http://www.example.com/php/uploads/
ProxyPass / http://www.example.com:8080/dist/
ProxyPassReverse / http://www.example.com:8080/dist/
and then use htaccess rewrite rules for your uploads and api
you can read more about mod_proxy

Apache, Node, .htaccess

I have an apache webserver listening on port 80. With apache, a PHP/MySQL system based on Zend framework. And I also have a node server listening on port 3000.
When a client sends a request, always on port 80, it's therefore first handled by apache. I would like to apply the following rules before treating the request:
if content-type is "application/json" then
use apache web server
else if content-type is "application/zend" then
use apache web server
else
use node server
Here content-type is sent in the request headers. Content-type "application/zend" is a custom content-type to say that, for this type of particular request, we don't want to use node server (I need this for some reasons).
I've tried to modify httpd-vhosts.conf with
ProxyPreserveHost on
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
But that's of course not enough as not everything should be handled by the node server (listening on port 3000). Hence some rules should be added. But I'm unsure where/how. I also tried to change the .htaccess file, but not sure how either.
Any help would be great! Thanks!
This should work (in .conf file):
RewriteEngine on
RewriteCond %{HTTP:Content-type} !=application/json
RewriteCond %{HTTP:Content-type} !=application/zend
RewriteRule ^ http://%{HTTP_HOST}:3000%{REQUEST_URI} [P]
Keep in mind that this might carry a performance penalty, and if most of your requests end on node, we should perhaps search for better solution.

creating subdomain(sub.domain.com) and redirecting to another server (sub.domain.org) whiles maintaining .com in the url

Redirecting to different server but same domain.
I have 2 servers on different hosts. One has domain.com and the other domain.org. domain.org has more resources. Now i want to create a sub domain on the first one (sub.domain.com) and redirect to (sub.domain.org) whiles maintaining the .com in the urls.
Thanks.
It depends on what's your hosting running on (Apache, nginx etc...).
In nginx you can use proxy_pass to redirect request to another host while still preserving url:
location / {
proxy_pass http://domain.org:80;
proxy_set_header X-Real-IP $remote_addr;
}
Take look at nginx docs for more info: http://wiki.nginx.org/HttpProxyModule
Or this discussion can be helpful: http://www.webmasterworld.com/apache/4464836.htm
It may be possible to also do this by manipulating DNS records so sub.domain.com is resolved to host where subd.domain.org is running (but you need to handle HTTP Host header in webserver)
There are several ways to do it:
You need to create DNS alias aka CNAME record for sub.domain.com for redirecting to sub.domain.org (look here ( http://en.wikipedia.org/wiki/CNAME_record ) for more info). You won't need to do any moves on .com server.
You can redirect with web-server configuration. So sub.domain.com will refer to .com server and web-server will redirect it to .org. For example virtual host in apache it will look like that.
ServerName sub.domain.com
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://sub.domain.org/ [R=302,L]
Last way is redirect via website files. For example you can put index.php (if you're using php) and place in it:
Or proxy it via web-server like `nginx`. upd. as #intense suggested.
ps. I think 302 is the right variant. Not 301 =)

RewriteRule in .htaccess

I would like to change the link "http://blog.test.com/" to "http://www.test.com/blog/".
I've tried the following code in my .htaccess
RewriteRule ^blog.test.com?$ test.com/blog [NC,R=301,L]
Did I miss anything? Thanks
If you're using apache, you need to match the host part of the url (e.g. blog.test.com) in a RewriteCond:
RewriteCond %{HTTP_HOST} ^blog.test.com$ [NC]
RewriteRule ^(.*)$ http://www.test.com/blog/$1 [R=301,L]
first of all, you must replace http://blog.test.com/whatever_or_empty to http://www.test.com/blog/whatever_or_empty in your HTML hrefs.
blog.test.com although a sub domain, is a different URL. i.e. when a RewriteRule does a rewrite to another URL an external redirect will occur. This will reflect in the browser. Be a temporary redirect(302(the default)) or permanent redirect(301).
So, using url rewriting to change the link http://blog.test.com/ to http://www.test.com/blog/ is useless.
Although, you can achieve this using Apache Module mod_proxy.
The Apache Proxy Modules has these:
mod_proxy: The core module deals with proxy infrastructure and configuration and managing a proxy request.
mod_proxy_http: This handles fetching documents with HTTP and HTTPS.
mod_proxy_ftp: This handles fetching documents with FTP.
mod_proxy_connect: This handles the CONNECT method for secure (SSL) tunneling.
mod_proxy_ajp: This handles the AJP protocol for Tomcat and similar backend servers.
mod_proxy_balancer implements clustering and load-balancing over multiple backends.
mod_cache, mod_disk_cache, mod_mem_cache: These deal with managing a document cache. To enable caching requires mod_cache and one or both of disk_cache and mem_cache.
mod_proxy_html: This rewrites HTML links into a proxy's address space.
mod_xml2enc: This supports internationalisation (i18n) on behalf of mod_proxy_html and other markup-filtering modules. space.
mod_headers: This modifies HTTP request and response headers.
mod_deflate: Negotiates compression with clients and backends.
You need at-least mod_proxy and mod_proxy_http modules enabled for the proxy to work:
you should have lines similar to these in your apache's conf file:
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_module modules/mod_proxy.so
use this in your Virtualhost of http://www.test.com
ProxyPass /blog http://blog.test.com
ProxyPassReverse /blog http://blog.test.com
ProxyRequests On
ProxyVia On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
Definitions:
ProxyPass Apache Docs.
ProxyPassReverse Apache Docs.
ProxyRequests Apache Docs.
Proxyvia Apache Docs.
You can also use a cache with mod_cache: mod_cache.
For more on caching, refer here: mod_cache Apache Docs.

Resources