htaccess skip all following rules for subdomain match - apache

I'm having problems with this answer https://stackoverflow.com/a/16800319/1283556
My current HTACCESS 301 redirects example.com to https://www.example.com, adding www. and https:// in as few redirects as possible.
I don't want it to add www. or https:// if the subdomain is m.example.com so I thought matching this condition and putting no rule, and using the [L] flag to stop doing future conditions would work.
RewriteEngine On
# Skip forcing www or https if the 'm' subdomain is being used for mobile
RewriteCond %{HTTP_HOST} ^m\. [NC]
RewriteRule - [L]
# Force www. and HTTPS
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
# Force HTTPS (www. was already there)
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Don't remap resources to the index processor, just add SSL
RewriteCond %{REQUEST_FILENAME} ^.*^.ico$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.css$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.js$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.gif$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.png$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.jpg$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.jpeg$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.xml$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.txt$$
RewriteRule ^(.*)$ https://$1 [R=301,L]
# Remap /page/filter/?query to /index.php?page=page&filter=filter&query
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/*([a-zA-Z0-9-]*)/?$ /index.php?page=$1&filter=$2 [QSA]
The effect of the above is:
example.com »301» https://www.example.com
m.example.com »301» https://www.m.example.com
The desired effect is:
example.com »301» https://www.example.com
m.example.com (do nothing, just serve content from example.com/mobile)
Server Version: Apache/2.4.10 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4 Apache
It's worth noting that I got this working as a solution but I consider it a work-around and not a linear solution:
RewriteEngine On
# Force www. and HTTPS
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^m\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
# Force HTTPS (www. was already there)
RewriteCond %{HTTP_HOST} !^m\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Don't remap resources to the index processor, just add SSL
RewriteCond %{REQUEST_FILENAME} ^.*^.ico$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.css$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.js$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.gif$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.png$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.jpg$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.jpeg$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.xml$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.txt$$
RewriteRule ^(.*)$ https://$1 [R=301,L]
# Remap /page/filter/?query to /index.php?page=page&filter=filter&query
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/*([a-zA-Z0-9-]*)/?$ /index.php?page=$1&filter=$2 [QSA]

Your original and updated question has two 301 redirect rules that are not good for both SEO rankings or user experience.
You can combine both rules i.e. http -> https and force-www into one and also take care of excluding m. host in the same rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?!m\.)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Make sure to keep this rule as very first rule and test it after completely clearing your browser cache.

Related

htaccess rewrite rule all existing files and directories in one domain go to same filepath in another domain

I'm trying to write an htaccess rewrite rule to send all existing files and directories at the domain http:// or https:// and www or not lun.re to lunre.com.
So lun.re/admin, which does exist would go to lunre.com, but lun.re/new, which doesn't exist would stay at lun.re, but would redirect to https://lun.re/new.
Here's what I have so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTP_HOST} ^lun\.re$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lun\.re$
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://lunre.com/ [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
Lines 5-8 were already there from my hosting provider.
I've also tried this:
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /loader.php [L,R]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{HTTP_HOST} ^(www\.)?lun.re$
RewriteRule ^ http://lunre.com/ [L,R]
But then I get a too many redirects error.
What exactly I am trying to do is if it does exist and the domain is lun.re then go to https://lunre.com/file-or-dir (the same filepath at the long url) otherwise if it doesn't exist then stay at the current domain and go to https://currentdomain/loader.php
You can use this:
RewriteEngine on
# http to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
#redirect all existing files/directories to http://lunre.com
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{HTTP_HOST} ^(www\.)?lun.re$
RewriteRule ^ http://lunre.com/ [L,R]
You don't need a RewriteRule to Redirect non-existent requests back to your main domain as the Rule only redirects existing files/folders.

www and non www resolution issues in my htaccess file

I have web site that I want to resolve the www and non www issues. I tried to using this below code from the .htaccess file. But no result.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.w15.lk%{REQUEST_URI} [R=301,L,NE]
# Remove Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L,NE]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
You were close with your attempt. Try and use this instead:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.w15.lk/$1 [L,R=302,NC]
I've set the R flag as 302 to make it a temporary redirect for testing. If this works, change it to 301 to make it permanent.
Don't forget to clear your cache before testing this.

incorrect redirect from http to https

trying to go https, but when I use the following in my .htaccess I get redirect problem error
here is what I have in my .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP} on [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} https [OR]
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
#Uncomment lines below when uploading to the live server
RewriteCond %{HTTP_HOST} !^(www\.)example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ index.php [QSA,L,NC]
#Uncomment lines below when uploading to the live server
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://(www\.)?example.com/.*$ [NC]
RewriteRule \.(gif|jpg|png|tif|js|css|xls|xlsx|zip|rar|pdf|ods|ots)$ - [F]
Please help
Your first rule has many erroneous [OR] conditions and that is causing redirect loop.
Change your first rule to this:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]
And test after clearing your browser cache.

redirect with .htaccess rewrite rule to different folder on my server

I have been trying to redirect to a different folder with .htaccess when I hit a domain.
Here's my .htaccess redirect rule, can you tell me where am I wrong?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
So, what I want is, if someone visits racereadymotorsports.in, they redirect to /raceready/ directory
and on the other hand if they visit to annelies-slabbynck.com, they should redirect to /annelies/ directory
At present, this redirects twice for annelies-slabbynck.com which means that, I end up with:
http://www.annelies-slabbynck.com/annelies/annelies/ as my final url
I am running a shared hosting and do not have access to add a new configuration.
Please help.
You need to remove the [OR] flag and reproduce the same conditions with the other rule. Maybe something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
Or better yet, you can use these conditions instead: RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteCond %{DOCUMENT_ROOT}/raceready/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/raceready/$1 -d
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteCond %{DOCUMENT_ROOT}/annelies/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/annelies/$1 -d
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
Note that the R=301 flag in your second rule redirects the browser, unlike the first rule which only internally rewrites the request.

Force HTTPS on certain URLs and force HTTP for all others

I have a client project where I need to force HTTPS for a certain folder and force HTTP for all others. I can sucessfully enforce HTTPS for the folder I desire but then all links back to the rest of the site end up being through HTTPS. I'd like to have a rule which forces requests for anything 'not' in the secure folder to be forced back to HTTP. Here's what I have so far:
RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1
'my' is the name of the folder that I need to force HTTPS for.
Any ideas?
Update: I also tried:
RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
# Force HTTPS for /my
RewriteCond %{HTTPS} !=on
RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
# Force HTTP for anything which isn't /my
RewriteCond %{HTTPS} =on
RewriteRule !^my http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
# Remove index.php from URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1
But instead of requests for /my being forced through HTTPS they now just resolve to http://www.example.com/index.php/my
:?
Ah, of course. The problem lies in the fact that your rewrite ruleset will be reprocessed after it is transformed to index.php following the initial redirect. Using what you currently have, you need to additionally condition the redirections to make sure they don't get applied after the rewrite to /index.php/my.
Something like the following should do:
RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
# Force HTTPS for /my
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/my [NC]
RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
# Force HTTP for anything which isn't /my
RewriteCond %{HTTPS} =on
RewriteCond %{THE_REQUEST} !^[A-Z]+\s/my [NC]
RewriteRule !^my http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
# Remove index.php from URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1
Give the following a try, should work for you:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/my
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/my
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
This is something that works from an old client website and could be adaptable for your purposes:
#If https off and in the cart dir
RewriteCond %{HTTPS} =off [NC]
RewriteCond %{REQUEST_URI} ^/cart/(.*) [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/cart/%1 [R=301,L]
#If https on and not in cart dir
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/cart [NC]
#Above line actually used to read RewriteCond %{REQUEST_URI} !^/cart|media|images|thumbs|css|js [NC]
#to allow js/css/images to be served so there were no mixed ssl messages popping up to visitors
RewriteCond %{REQUEST_FILENAME} !index\.php$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Replacing cart with my perhaps
Just invert the conditions:
RewriteCond %{HTTPS} =on
RewriteRule !^my http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]