Non HTTPS URL adds extra WWW infront - apache

If you visit Non HTTPS version it will not redirect to the HTTPS version. Instead it will look like https://www.www.mywebsite.com and will not work. This is the code in my htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]

Remove www. from this line:
RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Or you can do it like this :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=301,L]

Related

How to redirect URL in .htaccess with condition?

I have domain with url https://reg.bmi.id. I'd like to make any user that type anything after the url is redirected to https://reg.bmi.id.
Example:
https://reg.bmi.id/admin will be redirected in to https://reg.bmi.id
https://reg.bmi.id/asidjadhqowidhqohuqw will be redirected in to https://reg.bmi.id
https://reg.bmi.id/asdjqoq/qdoqwun/qowidopq will be redirected in to https://reg.bmi.id
https://reg.bmi.id/contact/contact.php will be redirected in to https://reg.bmi.id
In exception, if the user precisely type https://reg.bmi.id/reg_pilot then it will not be redirected. It open the page of /reg_pilot
This is my current .htaccess which is located in root folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^reg\.bmi\.id$ [OR]
RewriteCond %{HTTP_HOST} ^www\.reg\.bmi\.id$
RewriteRule ^/?$ "https\:\/\/reg\.bmi\.id\/" [R=301,L]
reg/pilot is a reference to any other rsources on this domain. /contact/contact.php is not exist. Anything typed except /reg_pilot after the domain is should be redirected
I have little to no experience dealing about .htaccess any help is greatly appreciated
With your shown samples, could you please try following.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##Setting rewrite base here.
RewriteBase /
##Checking for non https requests and applying https to it with/without www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?reg\.bmi\.id$ [NC]
RewriteRule ^/?$ https://reg.bmi.id/ [R=301,L]
##Stopping/forbidding direct access of index.php here.
RewriteRule ^index\.php$ - [L,NC]
##Any non existing directory/file is served by php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !reg_pilot [NC]
RewriteRule ^ https://reg.bmi.id/ [R=301]
RewriteRule ^ index.php [NC,L]

htaccess remove www redirect to ssl but wrong url

I created an mvc framework with php. my only working main file is index.php.
Normally I redirect from http to https. I also remove the www domain, but when I enter with www, if my URL is for example
domain.com/admin/products/edit/1,
htaccess does this.
domain.com/index.php/products/edit/1
how can i solve this
Thank you
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
You should have your rules like this way, always keep https redirection rule at very first place only. Please clear your browser cache before testing your URLs(rest of the rules are from OP's tried rules + I have changed regex of matching hostname too here).
RewriteEngine ON
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^(?:www\.)(example\.com) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^ index.php [L]

How to remove index.php from www to non-www .htaccess redirects

When I go to
www.example.co/courses
I get redirected to
https://example.co/index.php/courses
I want to get redirected to
https://example.co/courses
What I have tried
Here is my .htaccess file at the moment:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.co [NC]
RewriteRule ^(.*)$ https://example.co/$1 [L,R=301]
</IfModule>
How do I remove the /index.php/ part of the redirected URL?
You've got your external redirects in the wrong place. They need to go before your internal rewrites (before your front controller). For example:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.twocan\.co [NC]
RewriteRule ^(.*)$ https://twocan.co/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If you have your external redirects after the internal rewrite then the request gets rewritten to /index.php/... which then gets turned into an external redirect (the rewrite process loops in .htaccess, so it only gets rewritten to index.php on the first loop).

.htaccess redirect behaviour

I have a .htaccess file which redirects the user to https:// and adds /de/index if nothing else is specified.
RewriteEngine on
RewriteRule ^(hotelaccess)($|/) - [L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/de/index [L]
RewriteCond %{HTTP_HOST} !^www\.mypage\.de
RewriteRule (.*) http://www.mypage.de/de/index [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [L,QSA]
but now if I enter https://www.mypage.de it get's redirected to https://www.mypage.de/index ? Why is that and what do I have to add to fix this?
Clarification: The default URL should always be https://www.mypage.de/de/index if entering with and without www and both http:// and https:// (also with or without www)
You need to put your redirects first, then your rewrites. You can also combine the two redirects into a single rule:
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$ [OR]
RewriteCond %{HTTP_HOST} ^mypage\.de$ [NC]
RewriteRule ^(.*)$ https://www.mypage.de/$1 [R,L]
# for no URI
RewriteRule ^$ /de/index [L]
# for hotelaccess
RewriteRule ^hotelaccess - [L]
# for everything else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&page=$2 [L,QSA]

.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]