.htaccess that rewrites only if conditions are met - .htaccess

Currently I am working on an Angular 4 project for my work. The one thing I have 0 experience in is the .htaccess file.
Because we are using Angular, we are have the following rewrite conditions:
## Normal Rewrite
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
However, I would like to have a redirect to maintenance.html if we are under maintenance.
I have found the following code for that:
## Maintenance rewrite
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintenance.html [R=302,L]
Is it possible to have something like:
Maintenance: true
If maintenance === true {
// Execute maintenance rewrite
} else
// Execute normal rewrite
Thanks in advance.

Related

3 rules don't work together (htaccess)

I have tried some different things but can't get this working.
What my code should do:
remove www
use https
if the url doesn't exist go to index.php
1&2 work for me
3 works for me
But when I add 1,2 & 3 together I get an error.
My code is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ /index.php [L]
Thank you :)
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule .* https://%1/$0 [R=301,END]
RewriteCond %{HTTPS} =off
RewriteRule .* https://%{HTTP_HOST}/$0 [R=301,END]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ index.php [NS,END]
Using a base is a good idea. Extra groups are not needed in PCRE. Only redirect non-empty URLs as you likely have index.php in your DirectoryIndex already.

Multiple htaccess rewrite rules

I'm using the below htaccess file.First one is working fine.But the second one is not working.
RewriteEngine On
RewriteBase /Epropertiesnew/
RewriteCond %{THE_REQUEST} /Properties(?:\.php)?\?project=([^&]+)&property=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /ViewNewProjects(?:\.php)?\?newproject=([^&]+)&url=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# PHP hiding rule
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([A-Z,0-9-]+)/([A-Z,0-9-]+)/?$ Properties.php?project=$1&property=$2 [NC,L,QSA]
RewriteRule ^([A-Z,0-9-]+)/([A-Z,0-9-]+)/?$ ViewNewProjects.php?newproject=$1&url=$2 [NC,L,QSA]
If I commented the first one,second one is working fine.Please help me to fix this.

Strange behavior when checking %{REQUEST_FILENAME} in htaccess

I'm confusing with my htaccess configuration. This is my site's structure:
\www
\public
\css
\js
In www directory, I have a htaccess like this:
RewriteEngine On
RewriteBase /
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www\.example\.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{REQUEST_URI} !^/public/.
RewriteRule ^(.*)$ /public/$1 [NC,L]
And this one is in public directory:
RewriteBase /public/
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ - [NC,L]
When I access css directory, browser address bar will shows in 2 different ways:
www.example.com/css -> www.example.com/public/css/
www.example.com/css/ -> www.example.com/css/
Anyone can explain to me this difference and how can I configure to make both of them will return the second result www.example.com/css/
This is happening due to mod_dir adding a trailing slash after your rewrite rules. Have your root .htaccess like this:
RewriteEngine On
RewriteBase /
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R,L,NE]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{DOCUMENT_ROOT}/$1 !-d [NC]
RewriteCond %{DOCUMENT_ROOT}/$1/ !-d [NC]
RewriteRule ^((?!public/).*)$ public/$1 [NC,L]

How can I configure htaccess for https-rewriting?

I use https:// for some subdirectories (/login, /admininstration), for this I find some examples - but on the other hand I've to ensure, that all other pages redirect to http://.
Now, I send the user via links to https:// but when they leave the login-Area, they stay in https:// Mode.
I use now RewriteCond in standardconfiguration:
SetEnv APPLICATION_ENVIRONMENT show
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.xxxseitexxx.de$ [NC]
RewriteRule ^(.*)$ http://www.xxxseitexxx.de/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
RewriteRule !\.(js|ico|gif|jpg|jpeg|png|css|inc|pdf|csv|zip|rm|mp3|swf)$ index.php
Thanks in advance!
You can use this rule on top of other rules:
# force HTTP for not login or admininstration
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/admininstration|login/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
# force HTTPS for login or admininstration
RewriteCond %{HTTPS} off
RewriteRule ^(admininstration|login)/ https://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R=302]

Using Subdirectory as subdomain in htaccess

I am trying to point a subfolder on my site as a subdomain. for example, i want to turn www.domain.com/test to test.domain.com. I already have some rewrites in htaccess done by developers that we used earlier in the site building process, and I'd like to be able to add this line without interfering with those.
I've tried dozens of answers to this issue i found across the net, but none of them seemed to work and i'm not sure if it's due to my lack of knowledge of this language or if there's a conflict with something else.
The latest lines I tried are:
RewriteCond %{HTTP_HOST} !^www.domain.tv
RewriteCond %{HTTP_HOST} ([^.]+).domain.tv
RewriteRule ^(.*)$ /var/www/www.domain.tv/test%1
This is my current htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#RewriteCond %{HTTP_HOST} !www\.domain\.tv$ [NC]
#RewriteCond %{HTTP_HOST} ^(.+)\.domain\.tv$ [NC]
#RewriteRule .* index.php?option=com_contushdvideoshare&view=player&Itemid=33 [L]
RewriteCond %{REQUEST_URI} ^/album\/t\/([a-zA-Z0-9]+) [NC]
RewriteRule ^album/t/(.*) index.php?option=com_usermenu&task=directdownload&id=$1&type=one$
RewriteCond %{REQUEST_URI} ^/album\/([a-zA-Z0-9]+) [NC]
RewriteRule ^album/(.*) index.php?option=com_usermenu&task=directdownload&id=$1 [L]
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} (/[^.]*|\.(php|html?|feed|pdf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Can anyone help me here?
Try:
RewriteCond %{HTTP_HOST} !^www.domain.tv [NC]
RewriteCond %{HTTP_HOST} ([^\.]+).domain.tv [NC]
RewriteRule ^(.*)$ %1/$1 [L]
Subdomains aren't so easy to set up. You will need to configure your server before. More informations here : Create subdomains on the fly with .htaccess (PHP)

Resources