apache and node express with node rewrites - node.js

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]

Related

How to show a content of one website in another website?

I have a website for example: https://test1.com, and a page on that website https://test1.com/show-content.
I want that page to display the content from another website, for example https://test2.com/show-different-content.
I know I can do it with PHP and file_get_contents, but I'm trying to do it with .htaccess, since I understand it can be possible. I've looked through all the SO questions I found regarding that, but I found no clear solution.
What I have tried in .htaccess is the following:
<IfModule mod_rewrite.c>=
RewriteEngine On
RewriteRule ^show-content$ https://test2.com/show-different-content [P]
</IfModule>
What am I doing wrong? Or am trying to do something that is not possible?
You have two options for this if the owner of that second site actually grants permission to proxy his sites content. You can use the proxy module available for the apache http server.
Either direct:
ProxyPass /show-content/ https://test2.com/show-different-content/
ProxyPassReverse /show-content/ https://test2.com/show-different-content/
Or embedded in the rewriting module:
RewriteEngine On
RewriteRule ^/?show-content/(.*)$ https://test2.com/show-different-content/$1 [P]
Obviously the proxy module needs to be loaded and activated inside the http server.

htaccess works with www.example.com but not example.com

I have installed Ghost, which needs nodejs to run. I'm doing this on an Apache Linux server via managed hosting. They kindly let me login with SSH access so I've been able to setup nodejs and Ghost using the standard installation instructions. I installed ghost to the root of my domain so in normal operation someone would go to example.com and it'll show them my blog. Well that's what I'd hoped.
However now when I've come to load Ghost in my browser I discover because I'm accessing it the way I am, and that Ghost doesn't do server configuration, I seem to need an htaccess file to be able to make the site reachable.
So, I have created this htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ http://127.0.0.1:65515/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:65515/$1 [P,L]
</IfModule>
With that saved to the root, if I go to example.com/ghost (the admin panel for Ghost) it works. Nothing wrong there, looks great. If however I try and visit the root, ie goto example.com, instead of showing me the index, it shows me index.js - that is, it literally loads the contents of Ghost's index.js file and displays it instead of parsing it and displaying the main index of the website.
IF however I go to www.example.com then it all works. So whatever the problem is it's because I'm not using www. in the domain.
I would prefer it to work both with or without the www in the URL though. I did try adding some solutions to redirect non-www requests to www.example.com to th ehtaccess but for some reason it still doesn't work (as in if I type example.com it doesn't redirect me to www.example.com).
I think maybe you should be using mod_proxy rather than mod_rewrite. At least, that's what I've used in the past. Apache will catch requests coming in on port 80 and then redirect them to port 65515 where your node server is listening.
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypass
Like this:
ProxyPass / http://localhost:65515/
I figured it out. Wasn't anything to do with htaccess, or Ghost or nodejs or anything like it. No, instead the problem was the sodding server was caching the website. I discovered the setting to delete the cache and it all started working fine, so, this is now solved.

Rewrite URL to access files from directory off root, but not show in path

I would like to run a version of a Wordpress site on my localhost without going through too much hassle of changing url's, etc.
I have figured I will add a line in my hosts file to route domain.com to localhost. I just need a way to route / to look for files in /directory/directory/.
Thanks for any help.
Tried this, didn't seem to do anything:
RewriteEngine on
RewriteRule ^(.*)$ /directory/directory/$1 [L]
I used GasMask to route domain.com to my localhost.

Laravel 4 Routing issue

i hope you can help me out as been scratching my head for ages..
I have the laravel 4 installed in a folder called 'myapp' www.example.com/myapp/
I'm calling this but i'm getting app undefined..
Route::get('login', function(){
//do something
});
The only way i can get it to work is this way, but it messes everything up all the styles etc..
Route::get('/myapp/login', function(){
//do something
});
And this is my current .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
Options -MultiViews
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Does anyone know how to get it to work just for the myapp folder?
Thanks in advance.
I've now figured this out.
I needed to change the .htaccess file from this
RewriteRule ^ index.php [L]
to this
RewriteRule ^ /myapp/index.php [L]
I think your web server might be misconfigured. A correct web server configuration usually requires no changes to .htaccess.
Normally, using Laravel, you should have a DocumentRoot directive that points the web server to the public directory of your Laravel installation.
Exactly how you do that varies a little among operating systems. On my system, which is a development system running Ubuntu Linux, and which hosts multiple Laravel projects, I use multiple virtual servers. Ubuntu's configurations for these virtual servers are stored in /etc/apache2/sites-enabled. The configuration file for my calendar project (locally, calendar.dev) looks like this.
$ cat /etc/apache2/sites-enabled/calendar.dev
<VirtualHost *:80>
ServerName calendar.dev
ServerAlias calendar.dev *.calendar.dev
DocumentRoot "/var/www/calendar/public"
HostNameLookups double
</VirtualHost>%
If you're using "myapp" in place of the default "public" folder, then your DocumentRoot directive should point to "myapp" instead of "public".
If you're using "myapp" in place of the default "app" directory, you need to back up and rethink this. The "app" directory shouldn't be accessible to web users.
I agree completely with Mike Sherrill. Laravel ships with .htaccess config file that works "out of the box". There was some problems with older Laravel versions, but with L4 no. If you plan to add new virtual host don't forget to register one in /etc/hosts file. So check your server configuration.

.htaccess file 404 error with parallel plesk server

I have my .htaccess file working in localhost. But its not working if i upload it in server. It throws me 404 error.
I am using Parallel Plesk 11.0.9 and i can't find conf file for the same on that. If anyone has any idea how to fix it or any workaround for url rewriting would be great help.
Anyway here's the code in htaccess:
RewriteEngine on
RewriteRule ^store/living/Hutches-Armoires-Side-tables-Coffee-tables-Entertainment-centers? store.php?store=Living
RewriteRule ^store/dining/sideboards-buffets-chairs-benches-Dining-table$ store.php?store=Dining
RewriteRule ^store/working/Bookshelves-Study-tables$ store.php?store=Working
RewriteRule ^store/accessories/Boxes-Photo-Frames-Mirror-Frames-Block-Stamps-and-Book-stands$ store.php?store=Accessories
RewriteRule ^store/hallway/Console-tables-Armoires$ store.php?store=Hallway
RewriteRule ^store/sleeping/Bed-Frames-Night-Stands-Dressers-Mirror-framesBed-Linens-Canopies-Curtains$ store.php?store=Sleeping
You may need to wrap your rewrite rules with:
<IfModule mod_rewrite.c>
...
</IfModule>
...probably a good idea anyway.
Or you could try putting your rules into a vhost.conf file in the conf directory immediately below the location of your httpdocs directory. For example on a Centos machine the web root might be
/var/www/vhosts/domain.com/httpdocs
and you should have a:
/var/www/vhosts/domain.com/conf
directory, this will contain a set of pregenerated Apache config files that Plesk creates. If there isn't already create a vhost.conf and add your rules between a set of
<Directory /var/www/vhosts/domain.com/httpdocs/ >
... your rules ...
</Directory>
Once you've created the vhost.conf file you will need to tell plesk about it with
/usr/local/psa/admin/sbin/httpdmng --reconfigure-domain domain.com
If you still can't get it to work you can add a log for mod_rewrite, see this relevant SO answer for details
Is your Plesk running IIS and supporting PHP via FastCGI or ISAPI? If that is the case, check whether URL Rewrite is installed and follow this guide to translate htaccess (for apache) into web.config (for IIS)

Resources