Apache2 mod_rewrite rules - apache

How do i rewrite static files request like
http://example.com/assets/img/logo.png
to
http://assets.example.com/img/logo.png
main site rewrite rules
ServerName example.com
ServerAlias example.com
DocumentRoot /var/www/html/example.com/public/
<Directory /var/www/html/example.com/public/ >
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
RewriteEngine on
RewriteCond %{HTTP_HOST} !assets\.site\.com
RewriteCond %{REQUEST_URI} \.(png|gif|ico|css|js|tiff|woff|woff2|ogg|mp3)$ [NC]
RewriteRule ^(.*) http://assets.example.com$1 [NC,L]
sub-domain for assets is working properly
virtual host config for assets.example.com
ServerName assets.example.com
ServerAlias assets.example.com
DocumentRoot /var/www/html/example.com/public/assets/
<Directory /var/www/html/example.com/public/assets/ >
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
http://assets.example.com/img/logo.png gives a 404
but if i create a /var/www/html/example.com/public/assets/assets/ then http://assets.example.com/img/logo.png reutns 200

Per your comment, you just need to change the what you're capturing in the regex used by the RewriteRule. Right now $1 is the value of what's in (.*). You can remove "assets" from that by adding it to the path prior to your capture group.
Change:
RewriteRule ^(.*) http://assets.example.com$1 [NC,L]
To:
RewriteRule ^assets/(.*) http://assets.example.com/$1 [NC,L]

Related

Vuejs App still get 404 page after edit .htaccess

I have a Vuejs App deployed at apache2 server in digitalocean , when you hit the url it forwards you to login page which is working fine and then automatic navigate me to dashboard page that have cards when I click on one of cards I get 404 page even after I edit my .htaccess file in dist
NOTE the card route is domain.com/build
I tried .htaccess configuration from docs
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
and here is my virtual host configuration
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin email
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/public_html/app/dist
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPass /api/ http://localhost:8000/
ProxyPassReverse /api/ http://localhost:8000/
RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.
#RewriteCond %{SERVER_NAME} =domain.com
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
Nothing wrong with virtual host file, the problem was in apache2.conf file in /etc/apache2. what I need is to change this part from
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
to :
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
you need to change the AllowOverride attribute.

Combine two virtual hosts in one domain with a subdirectory

I have a website example.com served by Apache, and example2.com redirected to port 3001 (using NodeJS). It works with this config:
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /home/www/example
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Allow from all
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.example2.com
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) ws://localhost:3001/$1 [P,L]
ProxyPass / http://localhost:3001/
ProxyPassReverse / http://localhost:3001/
</VirtualHost>
Now I would like to have this (because I won't renew the domain example2.com):
example.com => served by Apache => /home/www/example
example.com/website2 => redirected to the NodeJS website using 3001 (the one previously served on example2.com)
Question: I'm about to use the following code, but is it a correct use of Directory?
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /home/www/example
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Allow from all
Require all granted
</Directory>
<Directory /website2/>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) ws://localhost:3001/$1 [P,L]
ProxyPass / http://localhost:3001/
ProxyPassReverse / http://localhost:3001/
</Directory>
</VirtualHost>
I think you should use the <Location> directives for section based URL for stable operation.
Each section directives have priorities by order and section based. (section applied order)
<Directory> is based physical file system and low priority if the same section layered by multiple directives, such as <Location>, <Directory>, <File> and so on.
For example,
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /home/www/example
<Directory /home/www/example>
Options FollowSymLinks
AllowOverride All
#Order deny,allow
#Allow from all
# above access control conf mean same with below conf
Require all granted
</Directory>
# Location is not necessary to mod_rewrite directives.
# rewrite conf can be coontrol the conditions with RewriteCond
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/website2/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule ^/website2/(.*) ws://localhost:3001/$1 [P,L]
# ProxyPass is now allowed into Location as following patterns,
# but 'ProxyPass http://localhost:3001/' is available into the Location directive but limited in Location URL pattern.
ProxyPass / http://localhost:3001/
ProxyPassReverse / http://localhost:3001/
</VirtualHost>

Several symfony project on one server with the same port

I have a trouble installing two projects on the same server. Some necessary information:
One VPS server
Two symfony projects (each in separate directory)
Subdomains pointing to projects (first.domain.com / second.domain.com)
All projects must be on the same port (80)
My server looks like this:
Root directory: /var/www/html
- index.php (welcome page)
- projecta (first project)
- projectb (second project)
I would like to achieve:
domain.com -> index.php
first.domain.com (domain.com/first) -> first project
second.domain.com (domain.com/second) -> second project
My apache config:
Alias "/first" "/var/www/html/projecta/web"
<VirtualHost *:80>
ServerName vps.net
ServerAlias http://vps.net/first
DocumentRoot /var/www/html/projecta/web
<Directory /var/www/html/projecta/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ first/app.php [QSA]
</IfModule>
</Directory>
ErrorLog /var/log/apache2/first_error.log
CustomLog /var/log/apache2/first_access.log combined
</VirtualHost>
Alias "/second" "/var/www/html/projectb/web"
<VirtualHost *:80>
ServerName vps.net
ServerAlias http://vps.net/second
DocumentRoot /var/www/html/projectb/web
<Directory /var/www/html/projectb/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ projectb/app.php [QSA]
</IfModule>
</Directory>
ErrorLog /var/log/apache2/second_error.log
CustomLog /var/log/apache2/second_access.log combined
</VirtualHost>
My problem is that first project override path to second project (if I change first project server, then second works).
Now looks like:
domain.com -> first project
domain.com/first -> first project
domain.com/second -> second project in web directory (when I click on the app.php then project run).
Solved
I solved the problem in this way (only in apache config file, I don't need any .htaccess file)
Alias "/first" "/var/www/html/projecta/web"
<VirtualHost *:80>
ServerName first.domain.com
ServerAlias *.first.domain.com
DocumentRoot /var/www/html/projecta/web
<Directory /var/www/html/projecta/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ first/app.php [QSA,L]
</IfModule>
</Directory>
ErrorLog /var/log/apache2/first_error.log
CustomLog /var/log/apache2/first_access.log combined
</VirtualHost>
Alias "/second" "/var/www/html/projectb/web"
<VirtualHost *:80>
ServerName second.domain.com
ServerAlias *.second.domain.com
DocumentRoot /var/www/html/projectb/web
<Directory /var/www/html/projectb/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ second/app.php [QSA]
</IfModule>
</Directory>
ErrorLog /var/log/apache2/second_error.log
CustomLog /var/log/apache2/second_access.log combined
</VirtualHost>
Currently alias didn't work, but for the moment it is enough.

Apache http-vhosts not the same as httacces?

I have a new project created in symfony and I configured httpd-vhosts to bypass all htaccess files that exist in the project's path.
<VirtualHost *:80>
ServerAdmin admin#lab.localhost
DocumentRoot "C:\wamp64\www\lab.localhost"
<Directory "C:\wamp64\www\lab.localhost">
AllowOverride None
AllowOverrideList None
Order Allow,Deny
Allow From All
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ /web [L]
</IfModule>
</Directory>
<Directory "C:\wamp64\www\lab.localhost\web">
AllowOverride None
AllowOverrideList None
Order Allow,Deny
Allow From All
DirectoryIndex app.php
Options FollowSymlinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
</IfModule>
</Directory>
ServerName lab.localhost
ServerAlias www.lab.localhost.com
ErrorLog "logs/lab.localhost-error.log"
CustomLog "logs/lab.localhost-access.log" common
</VirtualHost>
With this config it gives me Internal Server Error. If I set the AllowOverride and AllowOverrideList to All, it works as expected. The rewrite directves listed here are the same in my htaccess file.
Is there any difference between htaccess and httpd-vhosts?
And if not, why doesn't my config work?
Thank you

configure mod_rewrite on localhost windows xampp

I am trying to get mod_rewrite to work on localhost. If I go to localhost/website it loads the
DirectoryIndex home.php
and
localhost/website/home
also works
but for every other page it only shows the home page.
RewriteEngine On
RewriteBase /
RewriteRule ^home/?$ home.php [NC,L]
RewriteRule ^about/?$ about.php [NC,L] <--- this goes to home
http.cfg:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/xampp/htdocs/website"
<Directory "C:/xampp/htdocs/website">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>