Unwanted https redirect on subdomain with htaccess rule - apache

I have some sub-domains s1.mywebsite.com and s2.mywebsite.com. I'm trying to setup a URL rewrite rule to have any request intended for mywebsite.com be converted to https, while requests to the subdomain are unaltered.
Right now in the root of my main domain I have an htaccess file with the following:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule .* https://mywebsite.com/%{REQUEST_URI} [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
There's some content there for wordpress. As it stands, my server rewrites the request http://s1.mywebsite.com/page/ to https://mywebsite.com/page/ completely obliterating the subdomain. How can I have my rule ignore subdomain queries?

Add a condition to restrict first https->http rule for main domain only:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.)?mywebsite\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Related

How can I redirect all www traffic to non www url in vue project

I have the following code in my htaccess file which is supposed to redirect all www requests to the non www version of index.html, but its not working and I'm not sure why. Can anyone help?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
Have it like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
Here we are capturing value after www. in a capture group and then using that as %1 in target URL.
Make sure to clear your browser cache or use a new browser before you test this change.

.htaccess redirecting from http to https

This may have been asked before but I can't find an answer.
I have recently purchased an SSL certificate and I need help setting up the .htaccess file.
I am using the MVC design pattern so every page goes though index.php
This is my current .htaccess configuration and all pages go through https
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I have read that using https on the whole site adds overheads. Would it be possible to add rules for specific pages such as login/register/forgot password that would redirect to https instead of http?
To redirect specific pages to https:
RewriteEngine On
RewriteBase /
# specific pages to https
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} /(login|register|forgotPassword) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# all other pages to http
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/(login|register|forgotPassword) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

.htaccess redirect if domain = x.example.com

I have a site on a subdomain:
x.example.com
I put it on a subdomain because it's running on a separate server from the main site.
Using proxy for apache, I have now moved the site to example.com/x/ I want to make a 301 redirect from the subdomain like this:
Redirect 301 / http://example.com/x/
but I don't know how to make sure that it does not result in a redirect loop, as the page i'm redirecting to contains the same htaccess-file (as it's a proxy of the old file)
this is what I tried:
RewriteCond %{HTTP_HOST} ^x.example.com$ [NC]
RewriteRule ^(.*)$ http://staging.example.se/x/ [R=301,L,NE]
this is my full .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteCond %{HTTP_HOST} ^zenqa.herokuapp.com$ [NC]
RewriteRule ^(.*)$ http://staging.zenconomy.se/faq/$1 [R=301,L,NE]
</IfModule>
You're missing a backreference, you need a $1 to get the original request and pass it along to the redirect:
RewriteCond %{HTTP_HOST} ^x.example.com$ [NC]
RewriteRule ^(.*)$ http://staging.example.se/x/$1 [R=301,L,NE]

.htaccess redirect loop when trying to add forced HTTPS rule (Amazon Elastic Beanstalk)

I started receiving this error after trying to incorporate a rule to force HTTPS in the production environment. The BWC_ENV environment variable can have a handful of different values: "prod", "stage", "ben_local", "nam_local", etc.
Here's my .htaccess:
RewriteEngine On
# Force HTTPS
RewriteCond %{HTTPS} !=on
RewriteCond %{ENV:BWC_ENV} ^prod$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Parse the subdomain as a variable we can access in our scripts
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ /$1?subdomain=%1
# Ditto for the path; map all requests to /index.php
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !robots.txt
RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA]
# robots.txt - supply the correct one for each environment
RewriteRule ^robots.txt$ /robots.prod.txt [NC]
RewriteCond %{ENV:BWC_ENV} !prod
RewriteRule ^robots.prod.txt$ /robots.stage.txt [NC]
Edit
What's more, if my .htaccess only contains the following, this will cause a redirect loop too. Why could this be?
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It turns out this is an Amazon Elastic Load Balancer thing. You have to use Amazon's X-Forwarded-Proto header to accomplish this:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule !/status https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
You have L flag missing in few rules. Type changing your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# Force HTTPS
RewriteCond %{HTTPS} !=on
RewriteCond %{ENV:BWC_ENV} ^prod$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Parse the subdomain as a variable we can access in our scripts
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^([^.]+)\.[^.]+\.[^.]+$
RewriteRule ^(.*)$ /$1?subdomain=%1 [L,QSA]
# Ditto for the path; map all requests to /index.php
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !robots.txt
RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA]
# robots.txt - supply the correct one for each environment
RewriteRule ^robots.txt$ /robots.prod.txt [L,NC]
RewriteCond %{ENV:BWC_ENV} !prod
RewriteRule ^robots.prod.txt$ /robots.stage.txt [NC,L]

Apache Rewrite : not matching pattern not working

I want to redirect visitors to my main domain when they perform requests on my subdomain followed by a not matching URI.
For example, a visitor can access a resource at sub.domain.com/product/10 but he should be redirected to domain.com when he attempt to access other resources that not match product/:id on my subdomain like sub.domain.com/anOtherResource.
I have to do this with apache rewrite module. I found that ! operator can do the job but it's not working for me.
Here is my Rewrite configuration from .htaccess file :
RewriteCond %{HTTP_HOST} sub\.domain\.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com [L,R]
I also tested this configuration :
RewriteCond %{HTTP_HOST} sub\.domain\.com
RewriteRule !^/product/[0-9]+$ http://www.domain.com [L,R]
I don't know where is the mistake ...
[Edit]
The .htaccess file is configured for Wordpress. Here is the entire .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The problem was in the Wordpress Rewrite configuration so what's happened ?
Step 1
I perform a request at sub.domain.com/product/1 so it not match these RewriteCond :
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
Then, it not redirect to www.domain.com
Step 2
It continue the rewriting to the next Cond (Worpdress rewriting):
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
At this step, it send an Internal Redirect (not sent to the client) to sub.domain.com/index.php
Step 3
Because of the redirection, it apply again the previous RewriteCond
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]
It match the Cond, then it redirect the client to www.domain.com while I didn't expect it.
The Solution
I fixed the problem by adding a RewriteCond on index.php as showing bellow :
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]
Now it manage the Internal Redirection sent by the Worpress Rewriting.
The rewrite rules look ok, but try the following:
RewriteCond %{HTTP_HOST} ^sub[.]domain[.]com$
RewriteCond %{REQUEST_URI} !^/product/[0-9]+$
RewriteRule ^(.*)$ http://www.domain.com [L,R]
If this doesn't work, then you might need to copy and paste the entire contents of your VirtualHost configuration since it could be something else causing a problem.