I'm trying to run a small app I've wrote in nodejs on our server with forever. When I start my app like this:
forever app.js
In my folder /home/me/apps/myapp/ and the app is listening on port 61000
What should be the content of my .htaccess file under mydomain.me/myapp/?
Current .htaccess content (not working):
RewriteEngine On
# Redirect a whole subdirectory:
RewriteRule ^myapp/(.*) http://localhost:61000/$1 [P]
You should use Apache mod_proxy not mod_rewrite to run a Node.js app in Apache:
<VirtualHost :80>
ServerName example.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location /myapp>
ProxyPass http://localhost:61000/
ProxyPassReverse http://localhost:61000/
</Location>
</VirtualHost>
If you can't add a virtual host for your Node app you can try with htaccess and something like this:
RewriteEngine On
RewriteRule ^/myapp$ http://127.0.0.1:61000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/myapp/(.*)$ http://127.0.0.1:61000/$1 [P,L]
I will answer my own question, even though I think Michelems answer is also right.
My initial .htaccess file works.
RewriteEngine On
RewriteRule ^myapp/(.*) http://localhost:61000/$1 [P]
The thing I did wrong was creating actually the folder mydomain.de/myapp/ and put the .htaccess there. I have now the rewirte settings in my DocumentRoot/.htaccess, no public folder called myapp and it works fine.
Related
I have installed Codeigniter 4 on Raspbian and everything seems to be working fine.
My web directory is /var/www/html
Inside there are two folders containing two different Codeigniter-4 apps that I would like to invoke with:
blue.ddns.net -> /var/www/html/blue/public/index.php
black.ddns.net -> /var/www/html/black/public/index.php
So I'm creating a .htaccess file to put in the folder /var/www/html/ to handle the two requests
Well, I'm still at step 0 because I can't get the .htaccess file to work properly.
Below I attach a copy of the file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /var/www/html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blue/public/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
The Rewrite module is activated
the error that appears in /var/log/apache2/error.log is:
/var/www/html/.htaccess: Expected </IfModule> before end of configuration, referer: https://blue.ddns.net/
That shouldn't really be done in your htaccess. You should setup that in your apache virtual hosts.
Go into your sites-avaiable folder.
$ cd /etc/apache2/sites-available
Create a new virtual host called blue.ddns.net.conf
$ touch blue.ddns.net.conf
Open that file with nano or any other text editor you might like and add the following.
<VirtualHost *:80>
ServerAdmin your#email.com
DocumentRoot /var/www/html/blue/public
ServerName blue.ddns.net
ServerAlias blue.ddns.net
<Directory "/var/www/html/blue/public">
allow from all
AllowOverride All
Options None
Require all granted
</Directory>
RewriteEngine on
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteCond %{SERVER_NAME} =blue.ddns.net
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Save and close the file.
Then add this to your apache with the following:
$ sudo micro blue.ddns.net.conf
Repeat the same process for the other codeigniter install and reboot apache.
$ sudo service apache2 restart
That should do it.
Now if you want to override some config that would be something that you might do in your htaccess file for each codeigniter 4 install.
I've always worked in hosting and never had to do Apache configurations.
But now I'm discovering the Raspberry/Linux world.
Thanks to your answers I was able to better understand the problem and solve it (for the moment) in the following way:
Now my configuration files 000-default.conf (port 80) and 000-default-le-ssl.conf (port 443) both contain the following code:
<Directory "/var/www/html">.
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
in /var/www/html instead I created a .htaccess file with the following code:
# Disable directory browsing
Options All -Indexes
<IfModule mod_rewrite.c>.
RewriteEngine On
RewriteCond "%{HTTP_HOST}" "black\.ddns\.net"
RewriteRule ^(.*)$ black/public/index.php?/$1 [L]
RewriteCond "%{HTTP_HOST}" "blue\.ddns\.net"
RewriteRule ^(.*)$ blue/public/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
apps are located in
/var/www/html/blue
and
/var/www/html/black
It's a very simple solution that serves my purposes.
Although I think it's not very elegant
What do you think about it?
I have configured server with lucee tomcat and apache2 for virtual host on ubuntu. I have enabled rewrite rule and my virtual host is as followes.
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName example.com
ServerAlias example.com www.example.com
DocumentRoot /var/www/html/example.com/
<Directory /var/www/html/example.com/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error_main_example.log
CustomLog ${APACHE_LOG_DIR}/access_main_example.log combined
DirectoryIndex index.cfm
</VirtualHost>
redirect from htaccess file is working good but rewrite rule is not working. Here is the htaccess file that i am trying.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^(.*)/abc/([0-9]+)$ /test-404.cfm [L]
Here are the example URLs:
https://www.example.com/example.cfm/abc/2
https://www.example.com/example.cfm/abc/8
https://www.example.com/example.cfm/abc/15
It is showing me tomcat 404 error that
HTTP Status 404 – Not Found
Type Status Report
Message The requested resource [/example.cfm/abc/2] is not available
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.35
Can any body help me about this issue. By the way, site is configured using classic load balancer on AWS.
I'm posting a working solution here also, because you didn't answer to our posts of this cross post at the Lucee forum. This solution might also help others with the same problem.
The issue is that mod_proxy will always preced urlrewrite, unless you invoke mod_proxy with a special urlrewrite rule. To make urlrewrite work and use mod_proxy, you need to flag the rewrite rule with [P] (for proxy). Here is a working example that should work for you:
Step: Set everything in apache2.conf in mod_proxy.c as comment, just leaving ProxyPreserveHost and ProxyPassReverse, like so:
<IfModule mod_proxy.c>
ProxyPreserveHost On
#ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ http://127.0.0.1:8888/$1$2
#ProxyPassMatch ^/(.+\.cfml)(/.*)?$ http://127.0.0.1:8888/$1$2
# optional mappings
#ProxyPassMatch ^/flex2gateway/(.*)$ http://127.0.0.1:8888/flex2gateway/$1
#ProxyPassMatch ^/messagebroker/(.*)$ http://127.0.0.1:8888/messagebroker/$1
#ProxyPassMatch ^/flashservices/gateway(.*)$ http://127.0.0.1:8888/flashservices/gateway$1
#ProxyPassMatch ^/openamf/gateway/(.*)$ http://127.0.0.1:8888/openamf/gateway/$1
#ProxyPassMatch ^/rest/(.*)$ http://127.0.0.1:8888/rest/$1
ProxyPassReverse / http://127.0.0.1:8888/
</IfModule>
Step: In your virtual host configuration set the following rewrite rules ( that may work also in your .htaccess, but I’m not sure).
...
<Directory /var/www/html/example.com/>
...
...
RewriteEngine On
RewriteBase /
# Catch non-existing files/directories and pass them via proxy to a 404 cfml errorpage
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule "^(.*)$" "http://127.0.0.1:8888/my-404.cfm" [P]
# Catch https://www.example.com/example.cfm/abc/2
# Catch https://www.example.com/example.cfm/abc/7 etc.
RewriteRule "^example.cfm(/abc/[0-9]+)$" "http://127.0.0.1:8888/my-404.cfm" [P]
# Pass request for cfm/cfc files to proxy with mod_rewrite rule
RewriteRule "^(.+\.cf[cm])(/.*)?$" "http://127.0.0.1:8888/$1$2" [P]
...
</Directory>
I’ve tested it and the solution above works fine.
I have a symfony app in my /var/www/html/app/ and I'd like to access it via host/app/ but I can't make it work.
I have a conf file with this
<Directory /var/www/html/app>
AllowOverride All
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
</Directory>
It's commented because I tried to do the redirect here but it didn't worked so I made the following .htaccess.
And this htaccess in /var/www/html/app/.htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
With this .htaccess, When I go to ://host/app I go to the symfony app (not the apache directory structure) and that seems to be a good start.
But the problem is I'm getting an Exception No Routes found for /app/.
That's normal because /app/ is the subfolder in my server, not the start of symfony routes.
How can I make the symfony routes beginning after the /app/ folder path ?
I know there is some questions about symfony2/3 routing in subfolder but the answer requires to separate src and public files, that's not what I'd like to have.
I just want my routes working when accessing ://host/app.
For information, It works great when I access ://host/app/public/*Symfony routes*
Edit:
New app.conf
Alias /rapp "/var/www/html/app/public"
<Directory /var/www/html/app>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
#RewriteBase /app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!public/index.php)$ public/index.php$1 [L]
</IfModule>
</Directory>
I managed to resolve the problem !!!
I'm not sure the config is "proper" though but it seems to work.
Here is my working app.conf file (in /etc/apache2/sties-enabled/app.conf)
Alias /app "/var/www/html/app/public"
<Directory /var/www/html/app>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule (/public) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
</Directory>
The first RewriteRule is to prevent from internal Rewrite loop when I rewrite url like example.com/app/page-1 to example.com/app/public/index.php/page-1.
As page-1 is not a directory or a file it will loop infinitely.
So I assume as soon as I have a /public part, the rewrite worked and we stop the rewrite by doing nothing and end the rewrite with the [L] flag.
I'm not suire why I had to remove the public/ part in the "final" RewriteRule (RewriteRule ^(.*)$ index.php/$1 [L]) though.
If someone have ideas to improve this config, feel free to leave comment.
The problem is the that your web server's document root (the directory that apache will make public via the url) and your project's document root (the public directory) are not the same. Instead your server points to the project directory. That is why you have to manually go into the project's document root by adding the public/ to the url.
You should avoid making the project directory accessible as this could expose your configuration and thus make your website vulnerable, e.g. by giving people access to your database through the credentials inside the config.
You might be able to achieve your goal by storing the project somewhere else and then only symlink your project's public dir to /var/www/html/app. This will probably require some changes to your apache config, e.g. adding Options FollowSymLinks.
Another, more elaborate, approach could be using mod_proxy. The configuration will roughly look like this:
# Your public server reachable from the outside
<VirtualHost *:80>
ProxyPass /app http://127.0.0.1:8080/
</VirtualHost>
# Your internal server where the requests are proxied to
<VirtualHost 127.0.0.1:8080>
<Directory /var/www/html/app/public>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
...
</VirtualHost>
As you can see the internal web server points to your project's document root (public dir). You might have to do additional both in the apache setup and app, e.g. make sure trusted proxies are set correctly or adding ProxyPreserveHost On to the apache setup, but roughly this should point you in the right direction.
edit: I missed another obvious solution, using mod_alias to point your symfony app to /app:
Alias /app /var/www/html/app/public
I have what I thought to be a very simple and classic use-case of mod_rewrite that is stumping me.
I have a file structure like this:
/var/www/site
/var/www/site/webapp <-- js application
/var/www/site/index.php <-- php entry point
/var/www/site/.. other folders..
I want my users to navigate to "mysite.com" which will load the contents of the webapp directory, therefore I believe it should be the DocumentRoot. I cannot for the life of me get this working while keeping the rewrite for the PHP application/router functional.
My current VirtualHost configuration works, but the browser must be pointed to site.dev/webapp; navigating to site.dev shows a directory list.
<VirtualHost *:80>
ServerName site.dev
ServerAdmin me#localhost
DocumentRoot /var/www/site/
LogLevel core:debug
ErrorLog ${APACHE_LOG_DIR}/site.error.log
<Location />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [QSA,L]
</Location>
</VirtualHost>
Desired functionality is:
http://site.dev/ loads content from /webapp
http://site.dev/order/18 redirect to index.php passing /order/18 from URI
I'm trying to install my local symfony application on a remote server.
The application is currently installed in a public directory.
So I can access my app with www.mysite.com/public/web
To get rid off the public/web part, I created a .htaccess in the server root with the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.mysite.com$
RewriteCond %{REQUEST_URI} !public/web/
RewriteRule (.*) /public/web/$1 [L]
Now, I can access with www.mysite.com and it's work fine !
But, all the link and the assets have kept the public/web part.
It works, but I want to remove this part.
There is probably a configuration with the twig function path and asset, but I can't find it.
Any idea ?
Thanks.
Change your document root.
My vhost config looks like this
<VirtualHost *:80>
ServerName mysite.com
DocumentRoot /full/path/to/public/web/symfony-project/web
DirectoryIndex app.php
</VirtualHost>