I have a WordPress website (mywebsite.com) running on a shared hosting server. Alongside, I made a node.js app running in the backend on port 3000, which has been programmed to have different API endpoints.
I would like to have the endpoints to be in the same domain as the website in specific URLs.
The main endpoint, which has been declared in the node.js selector environment, works just fine. Let's call it mywebsite.com/myapp.
The other endpoints (which have been declared in the express routes) work correctly outside the website environment but are instead caught by apache/wordpress if I try to access them on the same domain even as sub-uri.
For example, if I try to access mywebsite.com/secondendpoint or mywebsite.com/myapp/thirdendpoint, the request gets caught by Wordpress which loads the 404 page.
Now, I understand that I have to instruct the apache server to redirect the requests to the above mentioned URL to the node.js app.
As long as I don't have access to the apache server, the only choice I have to tweak the .htaccess files.
When I created the node.js app, the virtual environment has been automatically set through a .htaccess file in its own subfolder in my website public folder, where my whole website is: /public_html/myapp . It uses Phusion Passenger to handle the virtual environment the app is running in.
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/user/apps/myapp"
PassengerBaseURI "/myapp"
PassengerNodejs "/home/user/nodevenv/apps/myapp/12/bin/node"
PassengerAppType node
PassengerStartupFile app.js
PassengerAppLogFile "/home/user/logs/myapp.log"
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END
The above code works just fine. I tried to add an Alias as suggested in some tutorial:
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION BEGIN
<IfModule Litespeed>
SetEnv Alias /secondendpoint /apps/secondendpoint
</IfModule>
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION END
In the root of /public_html/ I have the following .htaccess file
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^/secondendpoint/(.*)?$ http://127.0.0.1:3000/secondendpoint/$1 [P,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
No matter what I try, when I try to access mywebsite.com/secondendpoint, I always get the Wordpress 404 message.
What am I doing wrong?
I even tried to create a subfolder in /public_html/secondendpoint which actually does proxy the traffic, but I feel like this is not the right way to do it.
So, after a week of attempts, I found out that the syntax of the RewriteRule was indeed wrong for what I was trying to achieve.
RewriteRule ^/secondendpoint/(.*)?$ http://127.0.0.1:3000/secondendpoint/$1 [P,L]
The correct syntax is
RewriteRule ^secondendpoint(.*)$ http://127.0.0.1:3000/secondendpoint/$1 [P,L]
As shown in the comment of the .htaccess file "should only be modified via WordPress filters".
In fact is does automatically overwrites the rules I wrote every time it updates.
The right way to do it would be tweaking the internal WordPress WP_Rewrite
which I'm still trying to understand.
My best attempt has been adding to functions.php of my theme these lines:
function wpd_wtf_rewrite_rule() {
add_rewrite_rule(
'secondendpoint(.*)$',
'http://127.0.0.1:3000/secondendpoint$1',
'top'
);
}
add_action( 'init', 'wpd_wtf_rewrite_rule' );
These lines are being translated in
RewriteRule ^secondendpoint(.*)$ /http://127.0.0.1:3000/secondendpoint$1 [QSA,L]
and updated into the .htaccess file.
The slash in front of the target is an issue I don't know how to fix.
However, the generated .htaccess string does not proxy the requests resulting in errors from the API.
Related
We have a payment link system. Here's the htaccess file inside public_html/pay folder:
php_flag opcache.enable Off
RewriteEngine On
RewriteBase /pay/
RewriteRule ^result$ result.php [QSA,L]
RewriteRule ^([a-zA-Z0-9-_]+)$ index.php?url=$1
I converted it to NGINX configuration via getpagespeed, so here's the result:
rewrite ^/result$ /pay/result.php last;
rewrite ^/([a-zA-Z0-9-_]+)$ /pay/index.php?url=$1;
While there's no problem on pending payments, the expired ones are downloaded. For example, this link opens on both Apache and NGINX servers:
https://example.com/pay/M-168102-2432
But let's say this payment link has expired or paid or something else. Not active anymore. So it is redirected to:
https://example.com/pay/result?u=M-171824-2640&status=99
There's no problem on Apache, however, NGINX downloads result.php file (no file extension
though, just a file named "result".
I can't find the problem.
I am currently working to build a small php mvc framework. in a framework i have a this folder structure.
-app
--controllers
-Post.php
-core
-logs
-public
--.htaccess
-- index.php
-vendor
in here index.php is working as Front Controller
in post controller is look like this..
<?php
/**
* Posts controller
*
*/
class Posts
{
public function index()
{
echo 'Hello index';
}
public function addNew()
{
echo 'Hello addNew';
}
}
in url, i want to remove project/public/?posts/index public/?. When i remove (public/?) and visit the url. its showing me this error message.
project/posts/index
The requested URL was not found on this server.
using public/? project/public/?posts/index is working fine. and its echo index message
project/public/
The .htaccess inside of the public folder contains:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
in project main root folder ...
i did't added .htaccess and index.php file.
in .htaccess when i add this line. url redirect to xammp welcome screen
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
I'd say you want to internally rewrite all incoming requests to the controller inside the /project/public folder. But that is not what you do. The rule you implemented (RewriteRule ^(.*)$ index.php?$1 [L,QSA]) only rewrites relative to the requested folder. No mentioning of "public" in there.
The actual setup you need depends a bit on your http host setup here. Where its DOCUMENT_ROOT points to. Most likely to the folder that contains the file system structure you posted in your question. If so you should implement a rule that rewrites all incoming requests to the /project/public folder.
Something like that, though you probably need to tweak it to match your actual setup:
RewriteEngine on
RewriteRule ^ /public/index.php?%{REQUEST_URI} [L,QSA]
You can implement such rule in the http server's host configuration. Or, if you do not have access to that, you can use a distributed configuration file (if you have enabled those for the http host), so a ".htaccess" style file. That file should be located inside the folder your http hosts DOCUMENT_ROOT setting points to. So the folder containing the file system structure your posted.
Other setups are possible, this is just one option. The point is: you need to rewrite the requests to your controller. Where the controller actually is.
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.
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.
There is something strange going on. I am using Zend Framework on a subfolder in a site. I have a modular structure to my website, so the links consist of module names (www.xx.com/modulename). I have created a .htaccess file for the root dir, so that all of the requests would be routed to the public dir.
When i try to access the homepage ( www.xx.com) or any module it all goes exactly as it should. www.xx.com/authentication, www.xx.com/sample or www.xx.com/deathmetalreallyrox are all working as they should. But when I try to connect to www.xx.com/admin, it crashes and BURNS!!!! It does work however with www.xx.com/public/admin/.
Could it be, that my Hosting provider has set up some sort of rule in the httpd.conf to prevent me from accessing the admin section in my hosting?
Here's my .htaccess:
SetEnv APPLICATION_ENV development
RewriteRule ^(browse|config).* - [L]
ErrorDocument 500 /error-docs/500.shtml
SetEnv CACHE_OFFSET 2678400
SetEnv APP_DOMAIN http://www.xx.com/public
SetEnv APP_PREF /public
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Expires "Fri, 25 Sep 2037 19:30:32 GMT"
Header unset ETag
FileETag None
</FilesMatch>
RewriteEngine On
RewriteRule ^(adm|statistics) - [L]
RewriteRule ^admin/(.*) public/admin/$1
RewriteRule ^(.*)$ public/$1 [L]
Help?
EDIT:
Browser error msg:
Not Found
The requested URL /admin/ was not found on this server.
You redirect anything starting with admin/ to public/admin/ first and then everything to public/whatever. So when you request /admin/, it's trying to give you /public/public/admin/, which doesn't exist, so you get a 404.
Try removing the line RewriteRule ^admin/(.*) public/admin/$1. It's already handled by the next line, and you don't want to do it twice.
It turned out, that the server was somehow configured wrongly.
As I do not have the total control over the server, I couldn't know, that there was a rule in the httpd.conf, that denied access to any folder named admin or administrator, so that when I tried to get the contents of a folder by this name, Apache first checked, if a folder by this name existed, and denied it by default.
So, the concise answer, the server was configured wrongly, didn't allow access to specific folders.