.htaccess mod_rewrite not working in subdirectory - apache

I am trying to use htaccess to rewrite all requests to main.php in a subfolder on my domain. The code in my .htaccess is the following:
RewriteEngine on
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . main.php [L]
I am sure the module is enabled, I have checked using the apache2ctl -M command
root#vps847:/etc/apache2/sites-available# apache2ctl -M
Loaded Modules:
core_module (static)
log_config_module (static)
logio_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
alias_module (shared)
auth_basic_module (shared)
authn_file_module (shared)
authz_default_module (shared)
authz_groupfile_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
cgi_module (shared)
deflate_module (shared)
dir_module (shared)
env_module (shared)
mime_module (shared)
negotiation_module (shared)
php5_module (shared)
reqtimeout_module (shared)
rewrite_module (shared)
setenvif_module (shared)
ssl_module (shared)
status_module (shared)
Syntax OK
In my default site I have added/edited the following lines:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
<Directory /var/www/public>
AllowOverride All
</Directory>
I have restarted apache every time after editing something. Yet, everty time I surf to the public directory I see the apache directory listing. I have already tried to add a .htaccess to the root of my apache, but it didn't work either. When I add an error to my .htaccess file I do get an internal server error though.
Can someone find what is wrong with my configuration? I am using Ubuntu server 12.04.

Have this inside /public/.htaccess:
DirectoryIndex main.php
RewriteEngine on
RewriteBase /public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . main.php [L]

I was having this issue, and found the working solution to be:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /tools/admin
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /tools/admin/index.php [L]
Hope this helps someone

Related

Apache websocket connection:upgrade replaced by keep-alive

I am trying to figure out a mind-numbing apache issue where requests are going through two layers of apache reverse proxies before hitting upstream services. Most traffic seems to make it through fine. The notable exception is websockets.
In particular, this is a test request
curl -i -H 'Connection: Upgrade' -H 'Upgrade: websocket' localhost:80/test.html
When proxying requests from port 80 to port 8080, I notice (using tcpdump and Wireshark) that the Upgrade header has been removed and Connection: Keep-Alive has been set instead. Moreover, any attempts I have made at resetting the Connection header to Upgrade and Upgrade: websocket have been impotent.
Note that the upstream service needs Connection: Upgrade and Upgrade: websocket to initiate the websocket (I get a 404 error without those headers).
Why is Apache forcing Connection: Keep-Alive when proxying to itself? Is there any way to force it to pass along Connection / Upgrade headers or set those values manually? RequestHeader and friends have unfortunately not been helpful. Without Connection and Upgrade making it through the reverse proxy, the upstream service pukes and throws a 404/Not found at the websocket endpoint.
ServerRoot "/usr/local/apache2"
Listen 80
Listen 8080
LogLevel rewrite:trace8
LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule access_compat_module modules/mod_access_compat.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule filter_module modules/mod_filter.so
LoadModule xml2enc_module modules/mod_xml2enc.so
LoadModule proxy_html_module modules/mod_proxy_html.so
LoadModule mime_module modules/mod_mime.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule env_module modules/mod_env.so
LoadModule headers_module modules/mod_headers.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule version_module modules/mod_version.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule status_module modules/mod_status.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule auth_mellon_module modules/mod_auth_mellon.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule ssl_module modules/mod_ssl.so
<IfModule unixd_module>
User daemon
Group daemon
</IfModule>
<VirtualHost *:80>
LogLevel rewrite:trace8
ServerName localhost
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket
RewriteRule /(.*) ws://localhost:8080/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket
RewriteRule /(.*) http://localhost:8080/$1 [P,L]
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ProxyRequests Off
</VirtualHost>
<VirtualHost *:8080>
LogLevel rewrite:trace8
ServerName localhost
UseCanonicalName On
RewriteEngine On
ProxyPass / http://proxy-debug:8080/
ProxyPassReverse / http://proxy-debug:8080/
ProxyRequests Off
</VirtualHost>
ServerAdmin you#example.com
ErrorLog /proc/self/fd/2
DocumentRoot "/"
I do not completely understand how this works, but the nuggets of wisdom I have gleaned and the solution I have devised are as follows:
Apache does not understand websockets in one sense (i.e. when it is talking to itself). As a result, you need to use mod_rewrite and set the protocol to ws:// in order to forward websockets upstream (it seems that no amount of setting headers will help you here).
If you set the protocol to ws://, then Apache will set the appropriate headers (Connection: Upgrade and Upgrade: websocket) when proxying requests upstream. However, for some reason, it does not seem to do this when proxying requests to itself / another VirtualHost.
You can see a blurb in VirtualHost *:80 (copied below) that tries to discern whether a websocket is in order and then change the protocol accordingly. This will not work for VirtualHost *:8080 (see the above bullet). Other methods are necessary.
RewriteCond %{HTTP:Upgrade} =websocket
RewriteRule /(.*) ws://localhost:8080/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket
RewriteRule /(.*) http://localhost:8080/$1 [P,L]
Put all of this together, and you have to communicate to VirtualHost *:8080 that a websocket connection is needed upstream. Luckily, we are in control of VirtualHost *:80 and can discern this information / pass it along. We must do so without touching the Connection or Upgrade headers, since Apache does weird things with them. The protocol may be traceable, but I am not sure how to do that. As a result, I use a bogus, internal, custom header for the transmission. Probably best to name it in such a way that collisions are unlikely.
In VirtualHost *:80, we add a block like:
SetEnvIf Upgrade ^websocket$ websock=true
RequestHeader set X-Is-Websocket %{websock}e
And then we read that header in VirtualHost *:8080, changing the protocol if necessary:
SetEnvIf X-Is-Websocket ^true$ websock=true
RequestHeader unset X-Is-Websocket
# change protocol if necessary
RewriteCond %{ENV:websock} =true
RewriteRule /(.*) ws://proxy-debug:8080/$1 [P,L]
RewriteCond %{ENV:websock} !=true
RewriteRule /(.*) http://proxy-debug:8080/$1 [P,L]
Hope it helps! :)

Deploy Angular 2 to Apache Server

I want to deploy an Angular 2 application on an Apache server. I've read various guides like this and this but none of them is working. I have npm and ng installed on the server.
In a nutshell, here's what I did:
Cloned complete project repository on my server.
Installed dependencies using npm install.
Used ng build --prod command and it created a dist directory.
Changed apache root to /var/www/html/dist directory.
Enabled mod_rewrite, restarted apache and added this .htaccess in my dist directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
But only my home page domain.com works, other pages like domain.com/login, domain.com/register etc. throw 404 error. Even domain.com/index.html/login doesn't work.
The application works fine on my local system where I'm developing it using ng serve. What am i missing?
Create .htaccess file in the root folder and paste this in .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
It appears i was missing this in my /etc/apache2/sites-enabled/000-default.conf file. After adding this and restarting apache, website runs fine.
<Directory "/var/www/html/dist">
AllowOverride All
</Directory>
1) Change base tag in index.html file
<base href="./">
2) Build Project:
ng build --prod --base-href /myproject/
3) Add your dist files in "/usr/local/apache2/htdocs/myproject/"
4)On Apache Server 2.4 (httpd)
In File: /usr/local/apache2/conf/httpd.conf setup "FallbackResource"
<Directory "/usr/local/apache2/htdocs">
...
FallbackResource /myproject/index.html
</Directory>
full file "/usr/local/apache2/conf/httpd.conf":
ServerRoot "/usr/local/apache2"
Listen 80
LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule access_compat_module modules/mod_access_compat.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule filter_module modules/mod_filter.so
LoadModule mime_module modules/mod_mime.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule env_module modules/mod_env.so
LoadModule headers_module modules/mod_headers.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule version_module modules/mod_version.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule status_module modules/mod_status.so
LoadModule autoindex_module modules/mod_autoindex.so
<IfModule !mpm_prefork_module>
#LoadModule cgid_module modules/mod_cgid.so
</IfModule>
<IfModule mpm_prefork_module>
#LoadModule cgi_module modules/mod_cgi.so
</IfModule>
LoadModule dir_module modules/mod_dir.so
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
<IfModule unixd_module>
User daemon
Group daemon
</IfModule>
ServerAdmin you#example.com
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/usr/local/apache2/htdocs"
<Directory "/usr/local/apache2/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
FallbackResource /myproject/index.html
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog /proc/self/fd/2
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog /proc/self/fd/1 common
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"
</IfModule>
<IfModule cgid_module>
</IfModule>
<Directory "/usr/local/apache2/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule headers_module>
RequestHeader unset Proxy early
</IfModule>
<IfModule mime_module>
TypesConfig conf/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
</IfModule>
<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
</IfModule>
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
Change base tag in index.html file
<base href="./">
Create a build after that
ng build --prod
Now you will have a new folder dist, deploy dist folder now. It should work.
For Apache, to redirect any request to index.html, you need a .htaccess file in the root.
Just create a .htaccess in your dist folder (same level as index.html), I assume that's the public root of your app, and paste this in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# not rewrite css, js and images
RewriteCond %{REQUEST_URI} !\.(?:css|js|map|jpe?g|gif|png)$ [NC]
RewriteRule ^(.*)$ /index.html?path=$1 [NC,L,QSA]
Now, no matter what path you're requesting, Apache will always serve your index.html file, except requests to actual existing files (RewriteCond %{REQUEST_FILENAME} !-f) and requests to css, js etc. (RewriteCond %{REQUEST_URI} !.(?:css|js|map|jpe?g|gif|png)$) - which needed to be excluded, because you actually want those.
Also, the Apache's mod_rewrite extension needs to be enabled for this to work. Most often, it is enabled. If not, ask your hosting provider
Change base tag in index.html file
Run:
ng build --prod -bh "http://example.net"
open your index.html in dist directory after
ng build --prod
and Chang base element to your site DNS name for example for my local apache server
I changed from
<base href="/">
to
<base href="//localhost/angular2/ng2-cli/dist/">
I have create distribution directory as public. I have changed only virtual host setting of apache.
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName frontend.loc
DocumentRoot /var/www/frontend/public
<Directory "/var/www/frontend/public/">
Options FollowSymLinks
Allow from all
AllowOverride All
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.html
</IfModule>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
~
As per official Angular documentation for angular deployment (https://angular.io/guide/deployment)
A routed application should support "deep links".
We need to enable mod_rewrite module, there are various ways to do this. On an ubuntu machine we can run following commands:
sudo a2enmod rewrite
sudo systemctl restart apache2
we also need to allowoverride in configuration file so that .htaccess file can run:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Then in .htaccess file, copy following lines for rewriting:
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
If it still doesn't work then best solution is check error log, in my case I solved the issue by checking logs where I find that mod_rewrite module was not installed so I had installed mod_rewrite manually and it worked.

httpd : If REQUEST_URI contains specific word; need to append QUERY_STRING

I have to append a QUERY_STRING at the end of URL while redirecting to remote server on the basis of string found in the REQUEST_URI.
My httpd.conf looks like this :
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/site1$
RewriteRule ^/ /?Id=1 [QSA]
This is not working.
I want to add QUERY_STRING "?Id=1" if REQUEST_URI contains word "site1".
Pelase help.. Thanks.
Listen 9010
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_html_module modules/mod_proxy_html.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so
<IfModule proxy_html_module>
Include conf/extra/httpd-proxy-html.conf
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/site1$
RewriteRule .* ?tenantId=1 [QSA,L]
<VirtualHost *:9010>
ServerName localhost
ProxyPass /site1 http://localhost:7001
ProxyPassReverse /site1 http://localhost:7001
</VirtualHost>
I would be doing something wrong for sure.. Please help.
See if this works. If not, it should point you in the right direction.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/site1$
RewriteRule .* ?Id=1 [QSA,L]

RewriteRule in .htaccess doesn't work

I'm a newbie in Apache and I'm trying to shorten the URL of my pages, from
http://mysrvr:9191/my/long/path/to/app.php
to
http://mysrvr:9191/app.php
I thought it could be done with RewriteRule in an .htaccess file, but it didn't work at all, so I tried with a very simple test.
This one doesn't work either.
I will talk about this test.
This is .htaccess in my application folder:
DirectoryIndex robbie.html index4.html
Options +FollowSymLinks
#Edited --- RewriteEngine On
RewriteEngine On
RewriteRule ^(.*)/index4.html$ $1/robbie.html
I tried also
RewriteRule ^(.*)/index4.html$ /$1/robbie.html
But when introduced
http://mysrvr:9191/my/long/path/to/index4.html
none of them two options redirected to
http://mysrvr:9191/my/long/path/to/robbie.html
DirectoryIndex, however, works as expected so I discard the idea of .htaccess being ignored.
This made me think that perhaps mod_rewrite wasn't uploaded but:
> httpd.exe -t -D DUMP_MODULES
[Wed Aug 20 12:04:15 2014] [error] (OS 11004)The requested name is valid, but no data of the requested type was found. : Could not resolve host name hserver.rob.igroup -- ignoring!
Loaded Modules:
core_module (static)
win32_module (static)
mpm_winnt_module (static)
http_module (static)
so_module (static)
actions_module (shared)
alias_module (shared)
asis_module (shared)
auth_basic_module (shared)
authn_default_module (shared)
authn_file_module (shared)
authz_default_module (shared)
authz_groupfile_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
cgi_module (shared)
dir_module (shared)
env_module (shared)
include_module (shared)
isapi_module (shared)
log_config_module (shared)
mime_module (shared)
negotiation_module (shared)
rewrite_module (shared)
setenvif_module (shared)
php5_module (shared)
Would anyone please share their ideas on this issue?

mod_rewrite is not working - at all

This is driving me crazy.. Can't figure out whats wrong here..
Can't get mod_rewrite to work.. Nothing..
So here I try to redirect everyting to google.com
apache.conf (update)
<VirtualHost *:80>
ServerName www.domain.com
DocumentRoot /var/www/domain.com/public/www
RewriteEngine on
RewriteRule ^ http://www.google.com/ [L,R=301]
</VirtualHost>
apache.conf
RewriteEngine on
# Test
RewriteRule ^ http://www.google.com/ [L,R=301]
# redirect non-active subdomains to 'www'
RewriteCond %{HTTP_HOST} \.domain.com$ [NC]
RewriteCond %{HTTP_HOST} !^(sub|www)\. [NC]
RewriteRule ^ http://www.domain.com [L,R=301]
# put 'www' as subdomain if none is given
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [L,R=301]
# www.domain.com
<VirtualHost *:80>
ServerName www.domain.com
DocumentRoot /var/www/domain.com/public/www
</VirtualHost>
# sub.domain.com
<VirtualHost *:80>
ServerName sub.domain.com
DocumentRoot /var/www/domain.com/public/sub
</VirtualHost>
Not working
Here you can see loaded modules
root#dyntest-amd-3700-2gb /var/www/dyntest.dk # apache2ctl -M
apache2: apr_sockaddr_info_get() failed for dyntest-amd-3700-2gb
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[Thu Jan 30 14:33:34 2014] [warn] NameVirtualHost *:80 has no VirtualHosts
Loaded Modules:
core_module (static)
log_config_module (static)
logio_module (static)
version_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
alias_module (shared)
auth_basic_module (shared)
authn_file_module (shared)
authz_default_module (shared)
authz_groupfile_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
cgi_module (shared)
deflate_module (shared)
dir_module (shared)
env_module (shared)
geoip_module (shared)
mime_module (shared)
negotiation_module (shared)
php5_module (shared)
reqtimeout_module (shared)
setenvif_module (shared)
status_module (shared)
expires_module (shared)
headers_module (shared)
rewrite_module (shared)
ssl_module (shared)
Syntax OK
Add the following to your .htaccess: ThisIsNotSupposedToWork!!!!. If that doesn't generate an internal server error, your AllowOverride is set to None.