Using mod_rewrite to resource outside of DocumentRoot - .htaccess

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

Related

Rewrite rule not working with apache2, lucee tomcat

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.

.htaccess rewrite rules misbehaving

ErrorDocument 404 /pageNotFound.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^profile/([0-9a-zA-Z-_]+)/message/?$ message.php?user=$1 [NC,L]
#
#RewriteRule ^profile/([0-9a-zA-Z-_]+)/([0-9a-zA-Z-_]+)/?$ profile.php?user=$1&content=$2 [NC,L]
#
RewriteRule ^profile/([0-9a-zA-Z]+)/?$ profile.php?u=$1 [NC,L]
#
RewriteRule ^story/([0-9a-zA-Z-_]+)/?$ blog_post.php?url=$1 [NC,L]
#
RewriteRule ^forums/questions/([0-9a-zA-Z-_]+)/edit/?$ question_edit.php?url=$1 [NC,L]
#
RewriteRule ^forums/questions/([0-9a-zA-Z-_]+)/?$ question.php?url=$1 [NC,L]
#
RewriteRule ^forums/contribution/([0-9a-zA-Z-_]+)/edit/?$ contribution_edit.php?url=$1 [NC,L]
#
RewriteRule ^forums/contribution/([0-9a-zA-Z-_]+)/?$ contribution.php?url=$1 [NC,L]
#
RewriteRule ^forums/tags/([a-z-]+)/?$ allTags.php?tag=$1 [NC,L]
#
Above is my .htaccess file and I am using WAMP.
At first when I did this, everything works fine, but later on, some URLs started misbehaving for example if I type in localhost/profile/debbie after the page finishes loading the url changes to localhost/profile/Debbie/?u=debbie, though the page contents work fine. I just don't know why. But if I do something like localhost/profile/the_king the url remains the same after loading.
But later on the if I do this localhost/profile/the_king I started getting an error message of undefined index u
Secondly I have a Subdomain under WAMP for my mobile site which uses an IP address of 192.168.106.1 and I use a duplicate of the above .htaccess file for it, but if I do this 192.168.106.1/profile/the_king it just get back my pageNotFound.php error page back.
This is really getting me angry because I watched some video tutorials and I did exactly what they did but I just don't know why mine is misbehaving
Edit here is my configuration for my Subdomain
# Tells Apache to identify which site by name
NameVirtualHost *:80
# Tells Apache to serve the default WAMP Server page to "localhost"
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot "C:/wamp/www/"
</VirtualHost>
# Tells Apache to serve Client 1's pages to "client1.localhost"
# Duplicate and modify this block to add another client
<VirtualHost 127.0.0.1>
# The name to respond to
ServerName localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/mobile/"
# A few helpful settings...
<Directory "C:/wamp/www/mobile/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost>
<VirtualHost 192.168.56.1>
# The name to respond to
ServerName localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/"
# A few helpful settings...
<Directory "C:/wamp/www/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost>
<VirtualHost 192.168.106.1>
# The name to respond to
ServerName m.localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/mobile/"
# A few helpful settings...
<Directory "C:/wamp/www/mobile/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost>

Rewriting from public_html to root

I try to read many of the answer regarding htaccess but I could not find my way out.
Image this architecture in webhosting: root/public_html/subfolder
Now with the following code in htaccess file (placed in public_html) I can manage that when the domain is type in browser the visitor land in subfolder without digit example.com/subfolder, example.com is enough. It works.
What I would like now is that the visitor will be redirect not in subfolder but in an "extrafolder" placed inside the root (level root that is the same level where the folder public_html is placed but not inside public_html).
So how to modify this code:
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subfolder/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subfolder/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ /subfolder/index.html [L]
I try changing the /subfolder/ with ../extrafolder/ (supposing the double dot will push up one level) but does not work.
Any idea?
Thank you
The current configuration you put is just working in case you put .htaccess inside the root folder of your website, which in this case is public_html, so no way to access another folder outside that root, except if you have directory alias.
In case you have access to change your apache configurations
You need to have the following steps to completely control the root of your website.
1- Yur apache virtual host configuration (this should be in /etc/apache2/sites-enabled/example.conf) should be something like the following:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/test
<Directory /var/www/test/>
DirectoryIndex index.php
AllowOverride All
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
3- You need to add this Alias /externalfolder/ "/var/www/test/externalfolder/" to the virtual host configuration above
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/test/public_html
Alias /externalfolder/ "/var/www/test/externalfolder/"
<Directory /var/www/test/public_html>
DirectoryIndex index.php
AllowOverride All
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Now you can access that via http://example.com/externalfolder/file.php

Redirecting with htaccess to nodejs app

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.

Proper way to remove web directory from the path twig function in the url

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>

Resources