htaccess exclude URL from Rewrite - .htaccess

I've got some simple redirect for maintarance mode like:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js|svg)$
RewriteRule .* /index.html [L,R=302]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
It's working fine.. But when i want to exclude one more URL like:
RewriteCond %{REQUEST_URI} !^/wp-admin($|/)
It's don't redirect properly...

I've add RewriteCond for IP, just for my network.. Work's fine.
RewriteCond %{REMOTE_ADDR} !^123.123.123.123

Related

HTACCESS redirect controller folder to subdomain

I am stuck at a problem with .HTACCESS FILE.
I am using CodeIgniter 3
Scenario:
We have a domain https://example.com and the admin panel is in a folder like
application/controllers/manage/<controllers here>
Now I need this to happen.
If someone goes to https://example.com/manage should be redirected to
https://admin.example.com/manage.
Keep in mind that both subdomain & main domain points to same directory.
Also I want this to happen:
If someone goes to any url like
https://admin.example.com/<anyhting-other-than-manage>
to redirect to
https://example.com/<anything-other-than-manage>
Here is my current .htaccess
# gtranslate config
RewriteRule ^(af|sq|am|ar|hy|az|eu|be|bn|bs|bg|ca|ceb|ny|zh-CN|zh-TW|co|hr|cs|da|nl|en|eo|et|tl|fi|fr|fy|gl|ka|de|el|gu|ht|ha|haw|iw|hi|hmn|hu|is|ig|id|ga|it|ja|jw|kn|kk|km|ko|ku|ky|lo|la|lv|lt|lb|mk|mg|ms|ml|mt|mi|mr|mn|my|ne|no|ps|fa|pl|pt|pa|ro|ru|sm|gd|sr|st|sn|sd|si|sk|sl|so|es|su|sw|sv|tg|ta|te|th|tr|uk|ur|uz|vi|cy|xh|yi|yo|zu)/(.*)$ /gtranslate/gtranslate.php?glang=$1&gurl=$2 [L,QSA]
RewriteRule ^(af|sq|am|ar|hy|az|eu|be|bn|bs|bg|ca|ceb|ny|zh-CN|zh-TW|co|hr|cs|da|nl|en|eo|et|tl|fi|fr|fy|gl|ka|de|el|gu|ht|ha|haw|iw|hi|hmn|hu|is|ig|id|ga|it|ja|jw|kn|kk|km|ko|ku|ky|lo|la|lv|lt|lb|mk|mg|ms|ml|mt|mi|mr|mn|my|ne|no|ps|fa|pl|pt|pa|ro|ru|sm|gd|sr|st|sn|sd|si|sk|sl|so|es|su|sw|sv|tg|ta|te|th|tr|uk|ur|uz|vi|cy|xh|yi|yo|zu)$ /$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^static.example.com [NC]
RewriteRule !^(assets|uploads)/ https://example.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^admin.example.com [NC]
RewriteRule !^(manage)/ https://example.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(manage)/ https://admin.example.com/manage%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# procedure was miss spelled
Redirect 301 /yellwo /yellow
# Redirect 301 ......
Please help
UPDATE: Following does seems to work for redirecting admin.example.com<NOT-MANAGE-URI> to example.com/<NOT-MANAGE-URI>
<IfModule mod_rewrite.c>
RewriteEngine On
#gtranslate code
# ........
RewriteCond %{HTTP_HOST} ^static.example.com [NC]
RewriteRule !^(assets|uploads)/ https://example.com%{REQUEST_URI} [R,L]
# REDIRECT ADMIN
RewriteCond %{HTTP_HOST} ^admin.example.com [NC]
RewriteCond %{REQUEST_URI} !^/manage/
RewriteRule (.*) https://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
.......
After this:
URL in browser https://admin.example.com/about-us redirects to https://example.com/about-us which is correct.
but writing https://admin.example.com/manage/ redirects to https://example.com/index.php/manage/
You can insert this rule at top of your .htaccess to redirect example.com/manage to admin.example.com/manage:
RewriteCond %{HTTP_HOST} ^(?:www\.)?.(example\.com)$ [NC]
RewriteRule ^manage(?:/.*)?$ https://admin.%1%{REQUEST_URI} [NC,R=301,NE,L]

Force http on two pages using the .htaccess

I would like to redirect to pages from HTTPS to HTTP using the .htaccess file.
I have added the code for one page but when I add it for page2 it gives the site a redirect error.
Here is my code that works for one page going from HTTPS to HTTP in the .htaccess and I also make sure the .htaccess is in that directory with the files I want to be redirected as well.
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/page1.php$ [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
RewriteCond %{SERVER_PORT} !80
RewriteCond %{REQUEST_URI} ^/page1.php$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R,L]
But when I add it like this below it doesn't work and I get a redirect error.
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/page1.php$ [NC]
RewriteCond %{REQUEST_URI} !^/page2.php$ [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
RewriteCond %{SERVER_PORT} !80
RewriteCond %{REQUEST_URI} ^/page1.php$ [NC]
RewriteCond %{REQUEST_URI} ^/page2.php$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R,L]
I would like the pages to go to HTTP only using the .htaccess file without error:
http://mywebsite.com/page1.php http://mywebsite.com/page2.php
I figured it out. The correct code I needed is below:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/(page2.php$|page1.php$) [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
RewriteCond %{SERVER_PORT} !80
RewriteCond %{REQUEST_URI} ^/(page2.php$|page1.php$) [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R,L]

.htaccess force ssl execlude some url(s)

I'm trying to redirect every request to "https" instead of any call to "example.org/forum/...", but hunderets of solutions did not work and I can't figure out why for example this approach does not work:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/forum/.*$
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://example.org$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/forum/.*$
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://example.org/$1 [R=301,L]
EDIT: I've tried this example now on a domain with just some folders, index.html files and a .htacess file with nothing but these two rules in the root and works like charm!
The point is that I try to achieve this on a domain with a cms underneath, it is a contao installation and there it does not work. I also tried it on a wordpress installation and there it does not work too!
So I think it has something to do with internal redirections, does anyone has a clue!? I can't find such a case on the web...
The RewriteCond directive REQUEST_URI is starting with /, you should make these rules change:
RewriteCond %{REQUEST_URI} !^/forum/.*$
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://example.org/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/forum/.*$
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://example.org/$1 [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomaim.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
RewriteRule ^$ folder name [L]
It works for me redirect all to https
Foldername is the folder after youdomain.com/foldername

Forcing http on homepage using .htaccess

I'm using .htaccess to force https on certain pages and http on other pages and it's working fine. But I need to force http on home page (example: http://website.com) and I don't know how to do that. I tried RewriteCond %{REQUEST_URI} ^/index.php/? but as I'm using drupal that didn't work.
This is the script I'm using:
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/register/? [OR]
RewriteCond %{REQUEST_URI} ^/admin/?
RewriteRule ^(.*)$ https://website.com/$1 [R,L]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{REQUEST_URI} ^/about-us/? [OR]
RewriteCond %{REQUEST_URI} ^/help/?
RewriteRule ^(.*)$ http://website.com/$1 [R,L]
Any help is appreciated
First, instead of RewriteCond %{SERVER_PORT}, you can use RewriteCond %{HTTPS}, which should be more reliable in detecting HTTPS.
The homepage has the RewriteRule pattern ^$. So the rule would be
RewriteCond %{HTTPS} =on
RewriteRule ^$ http://website.com [R,L]
When everything works as you expect, you can change R to R=301.

Force HTTPS on rewritten url via htaccess

I have a website on www.domain.com and here is the root (and only) .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico)$ [NC]
RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I'm basically doing rewrite on my URL's so everything is going through the index.php page.
How can I force HTTPS (SSL) on www.domain.com/login, /register and /settings? So when I try to access the http://www.domain.com/login I should be immediately redirected to https://www.domain.com/login?
I searched SO and I found for folders so if I have folder /login it would work, but not for me in this case of already rewritten URLs.
Using mod_rewrite you should be able to do something like this
RewriteCond %{SERVER_PORT} !443
RewriteCond %{REQUEST_URI} ^/register
RewriteRule (.*) https://www.domain.com/register [R]
Try:
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^login$ https://www.domain.com/login [R=301,L]
RewriteRule ^register$ https://www.domain.com/register [R=301,L]
RewriteRule ^settings$ https://www.domain.com/settings [R=301,L]

Resources