url rewriting to redirect www to non www on https - apache

I just changed my website http to https. I'll already redirected http to https version. I also want to redirect www version to non www.
So far couldn't succeed it.
http://www.domain.com => https://domain.com
https://www.domain.com => https://domain.com
Here is the .htaccess file;
<ifModule mod_rewrite.c>
RewriteEngine On
redirect 301 /makale /sozluk
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)\.[\d]+\.(css|js)$ $1.$2 [L]
RewriteCond %{HTTPS ^www.sporapp.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
</ifModule>
Thank you.

You should remove this bit:
RewriteCond %{HTTPS ^www.sporapp.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
And add this just under the RewriteEngine On:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.sporapp\.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]
You need to have the redirecting happening before you rewrite, otherwise the request gets changed before it gets redirected.
Also, you have mod_alias directives which will interfere with the rewrite to /index.php:
redirect 301 /makale /sozluk
Should be:
RewriteRule ^makale/(.*)$ /sozluk/$1 [L,R=301]

Related

.htaccess non-www to www and http to https redirects

For some reason my redirects are not working. I just want non-www to go to www and non-https to go to https. Here's what I've got. It's from this post.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
EDIT: Here is my whole file.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} Off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} blog.example.com
RewriteRule ^(.*)$ https://www.example.com/blog/$1 [R=permanent,L]
RewriteCond %{HTTP_HOST} ^example.com/blog/$ [NC]
RewriteRule (.*) https://www.exmaple.com/blog/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example.com/blog/$ [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule (.*) https://www.example.com/blog/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I just want non-www to go to www and non-https to go to https.
What you had initially was close, but it depends on how the SSL cert is managed. If the SSL is managed by a front-end proxy (eg. Cloudflare flexible-SSL/FREE option) then it maybe OK. However, if the SSL cert is installed directly on your server then probably not.
Try something like:
# Non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R,L]
# Non-SSL to SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]
This assumes you only have example.com and www.example.com, and no other subdomains. However, in your edited code you make reference to a blog subdomain - this will be OK, provided you include the blog redirect first.
This is currently a temporary (302) redirect. Change the R to R=301 (permanent) only when you are sure it's working OK.
Make sure you browser catch is clear before testing.
Summary
The complete code would look something like:
# Redirect blog subdomain to main domain (required?)
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.example\.com
RewriteRule (.*) https://www.example.com/blog/$1 [R=301,L]
# Non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]
# Non-SSL to SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
It is best not to edit between the # BEGIN WordPress blocks to avoid your custom code being overwritten by WordPress.
Only include the RewriteBase directive once in your code (only the last instance will do anything). Currently, the RewriteBase directive is not actually doing anything anyway, since you have no relative path substitutions.

How to prevent subdomains from redirecting to https - htaccess?

I am trying to work out how to redirect all http instances to https:// though excluding the subdomain.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?example.co [NC]
RewriteRule (.*) https://example.co/$1 [L,R=301,QSA]
For example, domain.example.co should be left as it is and not redirected to https://.
example.co or example.co/sub/sub should be redirected to https://
I have tried changing the RewriteRule:
RewriteRule ^$ https://example.co/$1 [R,L]
This leaves subdomains as they are but has no effect on subdirectories under example.co - i.e. example.co/sub/sub.
How could I redirect from http to https but exclude all subdomains?
Note
I also have a rewrite rule which points subdomains to their directories without changing the URL:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.co [NC]
RewriteRule ^(.*)/?$ http://example.co/%1/$1 [P]
sub.example.co will displays example.co/sub but the URL will not change
You can try this :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(.+\.)?domain\.com$
RewriteRule ^(.*)$ https://%1domain.com/$1 [R,L]
Or This :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R,L]
Note: Replace sub with subdomain and domain with domain name
Enjoy it ;-)
Try below I am assuming you are not using www appended to host.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^example.co [NC]
RewriteRule ^(.*)$ https://example.co/$1 [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.co [NC]
RewriteRule ^(.*)/?$ http://example.co/%1/$1 [P]
ProxyPassReverse / http://example.co/

Redirect all from http to https except contact page

How do I redirect all pages on a site from https to http except the /contact page which I always want to be redirected to the https domain?
I can redirect all from http to https as follows:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
However, I'm having trouble adding the exception for the contact page which I want to always redirect from http to https
update
I've added the rules as suggested but /contact is now redirecting to /index.php?q=contact.
I'm using ModX which has the rules:
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Why is this conflicting with the new rule to redirect /contact (http) to /contact (https)?
Thanks :)
Use this:
RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} !^\/contact\/
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} \/contact\/
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
You can try this rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/contact$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^contact$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You can test your .htaccess file using this online tool (note that it doesn't includes all Apache functionalities and the result might slightly differ from your production server but works well for simple rules).
Try this:-
RewriteEngine On
RewriteBase /
//Turn https on for /page/contact
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/page/contact
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
//Turn https off everthing but /page/contact
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/page/contact
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
The first rule set will make sure the page /page/contact will redirected back to https if they are accessed via http. The second rule set will redirect all page to http that are accessed by https except /page/contact.

Redirect https to non http and Http to Https in directory with htaccess

I want to make my homepage to redirect to non https if it https
if https://example.com will redirect to http://example.com
and i want to make directory with ssl (just directory using SSL)
if http://example.com/directory wil redirect to https://example.com/directory
I'm use this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
but it just show loop
RewriteEngine On
RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_URI} !^/dir
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} ^off$
RewriteCond %{REQUEST_URI} ^/dir
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Mod rewrite, redirect all to https except specific URI and redirect that URI from https to http

So I've been hitting a wall have have tried many different options, and configurations. I'm trying to redirect my site to https with the exception of one specific URI which I want to be redirected from https to http and then remain http. I would greatly appreciate any help. This is being done from the virtualhost configuration. Below are the settings I have tried in pairs with ssl.conf and mysite.conf. However I keep either getting a redirect loop or a page that doesn't load.
mysite.conf
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !/example
RewriteRule ^(.*)$ https://my.site.com$1 [R,L]
or
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^(/|page1|page2)
RewriteRule ^(.*)$ https://my.site.com$1 [R,L]
or
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !=/example
RewriteRule ^(.*)$ https://my.site.com$1 [R,L]
or
RewriteCond %{REQUEST_URI} =/example
RewriteRule ^(.*)$ http://my.site.com$1 [R,L]
Plus one of the above.
Then in ssl.conf
RewriteEngine on
RewriteCond %{REQUEST_URI} =/example
RewriteRule ^(.*)$ http://my.site.com$1 [R,L]
This should do it for you. Place the most specific rule (that for /example) first.
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteRule ^/?example$ http://my.site.com/example [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/?example$
RewriteRule ^/?(.*)$ https://my.site.com/$1 [R=301,L]
This should work in either server (.conf) or directory (.htaccess) context.