We have our site running on HTTPS. Is there a way to accept HTTP request for URLs containing /public/? For all other HTTP requests they should be redirected to HTTPS.
I have RoR application running on apache + passenger.
EDIT
Since the assets (/public/) requests will explicitly be on HTTP, how about creating another VHOST to handle HTTP requests. And for any requests other than /public/ directly could be redirected to HTTPS? If we can go this way how can we set this up in VHOST for HTTP?
EDIT 2
I am sorry, I should have been elaborated this in first place. Here is our setup. There two separate applications. One is running on HTTPS (S) and other on HTTP (P). The app P fetches data (a full HTML page, call it page) from S and render to client. The CSS file used in page is located on 'S' so I need to HTTPS in CSS link. I want to use HTTP instead to refer the CSS.
You can use mod_rewrite and place a .htaccess file with the below contents in your DocumentRoot.
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
Rewritecond %{REQUEST_URI} .*/public/.* [NC]
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Rewritecond %{REQUEST_URI} !.*/public/.* [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
NameVirtualHost *:80
Listen 80
Listen 443
<VirtualHost *:80>
ServerAdmin username#somesite.com
DocumentRoot /pathto/DocumentRoot
ServerName domain.com
ErrorLog path/to/your-error_log
CustomLog path/to/your-access_log common
RewriteEngine on
RewriteBase /
Rewritecond %{REQUEST_URI} !.*/public/.*$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin username#somesite.com
DocumentRoot /pathto/DocumentRoot
ServerName domain.com
ErrorLog path/to/your-error_log
CustomLog path/to/your-access_log common
RewriteEngine on
RewriteBase /
Rewritecond %{REQUEST_URI} .*/public/.*$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#All the other directives pertaining to SSL add below
</VirtualHost>
Related
I am working with a project having its front-end in angular. I am having issue when deploying it to server. The project was deployed using jenkins and the project folder angProject1 was deployed in /var/www/html/dms folder.
The /etc/apache2/sites-available/dmsproxy.conf file have
ServerName dms.xxxxxx.com
DocumentRoot /var/www/html/dms/angProject1
I have added the .htaccessfile to /var/www/html/dms/angProject1:
<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 /etc/apache2/sites-enabled/000-default.conf contains :
<Directory "/var/www/html/dms">
AllowOverride All
</Directory>
I can access the dms.xxxxxx.com page, but when I try for dms.xxxxxx.com/opendms etc, it is not working (ERROR:The requested url was not found). Tried adding # as dms.xxxxxx.com/#/opendms too, but it redirects to dms.xxxxxx.com file.
The base href in index.html is <base href="./">.
Thanks for the help!
I solved the issue. Changed the
<Directory "/var/www/html/dms">
AllowOverride All
</Directory>
inside the /etc/apache2/apache2.conf and now the application is reading the .htaccess file. Also my .htaccess is changed to:
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
Changed the base href to <base href = "angProject1"/>
I am trying to configure my docker GitLab CE (latest) instance that works with an integrated letsnecrypt that is part of the docker, in order to authenticate it as HTTPS under a sub-domain that is proxied by an Apache 2.4.
The reason i want it to go through Apache is that In this environment there is a web page running which claims port 80, 443 etc. So, in order for me to have both, i have mapped docker's ports to 443:444 and 80:3000.
And via apache's virtual host naming i have the following configuration:
<VirtualHost domain.com:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost gitlab.domain.com:80>
ServerAdmin webmaster#localhost.com
ProxyPreserveHost On
ProxyPass "/" "http://public-ip:3000/"
ProxyPassReverse "/" "http://public-ip:3000/"
ServerName gitlab.domain.com
</VirtualHost>
<VirtualHost gitlab.domain.com:443>
ServerAdmin webmaster#localhost.com
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
ProxyPreserveHost On
ProxyPass "/" "http://public-ip:3000/"
ProxyPassReverse "/" "http://public-ip:3000/"
ServerName gitlab.domain.com
</VirtualHost>
What do you think is the best approach?
I am using this to redirect all requests for http pages to https. However, I need to exclude certain pages on the site that must remain http. I am at a loss how to do this.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.reny.net/$1 [R,L]
Use RewriteCond to check %{REQUEST_URI} before the RewriteRule
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^(/page1|/page2)$
RewriteRule ^(.*)$ https://www.reny.net/$1 [R,L]
I am running a simple rails application on port 3000. I have reverse proxied it to apache port 80 with mod_rewrite enabled. When I access via http and https the application works fine. Now I would like to redirect all http request to https. I already have a .htaccess file under public folder of the rails application but it doesn't seem to work.
RewriteCond %{SERVER_PORT} 80 [OR,NC]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]
Domain.com points to my domain. Also I don't want my subdomains to be redirected to https. How do I achieve it?
Remove the OR condition and try:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]
To check if https is enabled, you can use RewriteCond with the HTTPS variable
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L]
Pictures shows that I put on terminal:
sudo vim /etc/apache2/sites-available/fusion_invoice.dev.conf
After that at sitepoint.com says that I need to copy paste below:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/fusioninvoice
ServerName fusion.invoice.dev #change this setting according to your domain name
DirectoryIndex index.php
<Directory /var/www/fusioninvoice>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I do not know which path to change.
Help!
In the file you mentioned, the Apache config file, add the following code:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/fusioninvoice # <<< CHANGE THIS PATH TO THE ABSOLUTE PATH ON YOUR SERVER WHERE THE CODE IS STORED
ServerName fusion.invoice.dev #change this setting according to your domain name
DirectoryIndex index.php
<Directory /var/www/fusioninvoice> # <<< ALSO, CHANGE THIS PATH TO THE ABSOLUTE PATH ON YOUR SERVER WHERE THE CODE IS STORED. SAME AS ABOVE
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I hope it is clear now :)