.htaccess rewrite question - url

How would I change
http://website.com/file.php
to
http://website.com/file/
thanks.

In most apache server deployments, you can create a .htaccess on the document root containing:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php
This will work as long as "file" does not match any existing directory.

Related

Angular 5 deployment with Apache2

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"/>

http to https EXCEPT

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]

Redirect to https a rails application reverse proxied with apache using .htaccess

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]

Installing a program to my server

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 :)

Apache: Serving assests via HTTP while site is running on HTTPS

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>

Resources