8

I am new in htaccess. I am using Keystone.js, using which I developed a blog which is listening on port 3000. Like this:

https://localhost:3000

Everything is fine now. But what I want is that my blog should run on this url :

https://localhost/blog

How can I achieve this scenario where I provide "https://localhost/blog" and its should work like "https://localhost:3000". In this case URL will remain the same: https://localhost/blog

Plus I also want that when a user visits this url: "https://localhost:3000", it should redirect to: "https://localhost/blog".

How can I achieve this scenario? All I want is to hide a port from URL.

I have tried alot of things to work around but its not working for me. Something like this:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^blog(.*) http://localhost:3000/$1 [P,L]

Ok now this about code is working perfect for main page i.e: https://localhost/blog because I added a rule for it in htaccess. But the issue is that all links, images & included files are broken in my blog.

Now I used base tag for this issue. But it converts my links to this format:

http://localhost:3000/contact
http://localhost:3000/help
http://localhost:3000/post

I considered http://localhost:3000/ as a base tag in my page head. As you can see 3000 port is again appearing in urls which I dont want to show. It should be:

http://localhost/blog/contact
http://localhost/blog/help
http://localhost/blog/post
Irtiza shahid
  • 359
  • 2
  • 13
  • Are you running NodeJS on Apache? – JME Jun 10 '15 at 04:50
  • yes node and apache on Ubuntu server. – Irtiza shahid Jun 10 '15 at 05:04
  • What about using ProxyPass? Wouldn't it be better solution? – jackoob Aug 13 '15 at 15:25
  • When making use of ModRewrite for friendly URLS, the best practice I've found, for not breaking the page, is to make all relative URLs begin with a slash thus referencing the web root. This will work whether or not the user is visiting http://localhost/blog/help, http://localhost/blog, or http://localhost:3000/. As far as navigation links, if you want both `:80/` and `:3000/blog` to load the same, whether or not a redirect is in place, you'll need PHP to output either `/` or `/blog/` depending on the URL the user is visiting so the page doesn't break. – Ultimater Sep 09 '15 at 07:10
  • I think avoid the base element at all costs, if you can. It's terrible practice and has a tendency of causing more trouble along the way than it solves as the software life cycle continues. It's the easy fix, comparable to disabling PHP warnings rather than fixing them, when all relative URLs should be based on the web root or be absolute. URLs relative to the current page are terrible practice such that you shouldn't ever need to resort to the base element in the first place if you build the application right. – Ultimater Sep 09 '15 at 07:18
  • I have done this using Virtual Hosts in appache server . ProxyPass the subdomain (port:80) traffic to my blog (port 3000) . – Irtiza shahid Sep 11 '15 at 06:59

2 Answers2

1

This is the safest and easiest way to run Node on port 80:

Login to the server and issue the following commands:

$ sudo apt-get install libcap2-bin
$ sudo setcap cap_net_bind_service=+ep /usr/local/bin/node

Note: Change the path to Node above to whatever is displayed when you type which node

Now when you tell Node to run on port 80, it won't complain. And you won't have to deal with Apache or Nginx, run your app as root, or worry about port forwarding.

Kyle Anderson
  • 3,059
  • 1
  • 13
  • 19
1

I would advise to use NGINX to solve this issue. You can check those links :

https://allaboutghost.com/how-to-proxy-port-80-to-2368-for-ghost-with-nginx/

Node.js + Nginx - What now?

Community
  • 1
  • 1
rebe100x
  • 1,473
  • 15
  • 18