www to non-www with SSL without index.php - .htaccess

When I try to enter my website from url www.mypage.com I got something like http://www.mypage.com/index.php/https://mypage.com/https://www.mypage.com....
When I enter from url mypage.com I got https://mypage.com - AND ITS OK
When I enter from url mypage.com/stuff I got https://mypage.com/index.php/stuff (page is working)
When I enter from url www.mypage.com/stuff I got https://mypage.com/index.php/stuff
What I should do? I would like to have always url like https://mypage.com without index.php and without 'www'.
My .htaccess :
AddHandler application/x-httpd-php70 php
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\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Thanks!

Try switching the order around so the internal redirect to index.php/$1 executes last:
AddHandler application/x-httpd-php70 php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Related

How to redirect http to https://www.example.com using htaccess file?

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
I have created a website using Codeigniter framework and my domain and hosting is on GoDaddy. Now, I have installed SSL certificate and manage with my website. Now, when I use example.com in URL it redirects me on https://www.example.com but when I click on my login page it shows me Not Found and URL looks like https://www.example.com/index.php?/login but I want URL Like https://www.example.com/login. So, How can I do this? Please help.
Thank You
Change order of your rules
Have a single rule to redirect www and http -> https
Your .htaccess should be like this:
RewriteEngine on
# redirect for adding www and https
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Make sure to use a new browser for your testing.
Please use this htaccess rules will work 100%
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
use this:
Options All -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

redirect old url to new url of same domain using htaccess

I would like to redirect https://www.example.com/page/jobseekers to https://www.example.com/jobseekers
I used
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Redirect /page/jobseekers /jobseekers
But its redirecting into https://www.example.com/jobseekers?page/jobseekers.
Place this rule just below RewriteEngine On line:
RewriteEngine On
RewriteRule ^page/(jobseekers)/?$ /$1? [L,NC,R=301]
# other rules go below
Make sure to test in a new browser or clear browser cachhe before testing.

301 redirection through .htaccess file not working

I'm using CodeIgniter.
in httacess file, I wrote this code to remove index.php from url:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Now I want to redirect site root to subdomain, with this code:
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
domain.com redirected to sub.domain.com successfully. but domain.com/dir to sub.domain.com/dir not work and still opening domain.com/dir.
When I removed first code, redirection worked perfect, but I need to remove index.php too.
Adding $to the end of rewritecond %{http_host} ^domain.com [nc] should fix it for you.
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
Would become
rewritecond %{http_host} ^domain.com$ [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
Have your rules like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Codeigniter .htaccess file for SSL inserts index.php

I'm using the following .htaccess file to redirect certain pages to https://
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (sale|success|cancel)
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(static|sale|success|cancel)
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond $1 !^(index\.php|resources|robots\.txt|static)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
This works, except that when you go to the page that gets rewritten to https:// it also inserts index.php in the URL. Everything works fine, but I would like that index.php not to be inserted. Any ideas?
Thanks.
You are missing L flag from your top 2 rules.
You can use this code:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (sale|success|cancel) [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(static|sale|success|cancel) [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt|static) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
<Directory "/home/test">
Require all granted
***AllowOverride All***
SSLOptions +StdEnvVars
</Directory>
After adding "AllowOverride All" in your apache configuration, it will work properly.
Regards.

non www to www, tailing slashes, non https to https and friendly URL's

Hi getting a little stuck with my .htaccess file rules.
I have set up non https redirects to https (this works):
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
I have set up non www to www (this works):
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I have set up so it always adds trailing slashes to urls (this adds a trailing slash to thank you.php):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
Redirects (not working):
RewriteRule ^thank-you/?$ thank-you.php [L]
RewriteRule ^thank-you.php$ http://%{HTTP_HOST}/thank-you/ [L]
What should happen is if someone goes to any address that not www or not http they are redirected to https and www.
whet the file thank-you.php is accessed it should redirect to /thank-you/
if someone accessed /thank-you it should redirect to /thank-you/
getting a little stuck as the URL re writings are not working but look like they should.
thanks in advance
full code:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
RewriteRule ^thank-you/?$ thank-you.php [L]
RewriteRule ^thank-you.php$ http://%{HTTP_HOST}/thank-you/ [L]
Now whats strange is this code redirects /thank-you.php to /thank-you/ but does not redirect /thank-you to /thank-you/ i think something is clashing:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
RewriteRule ^thank-you/?$ thank-you.php [L]
RewriteRule ^thank-you.php$ http://%{HTTP_HOST}/thank-you/ [L]
i got round my issue with the bellow:
htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
php:
function CheckUrl ($s) { // Get the current URL without the query string, with the initial slash
$myurl = preg_replace ('/\?.*$/', '', $_SERVER['REQUEST_URI']); //If it is not the same as the desired URL, then redirect
if ($myurl != "/$s") {Header ("Location: /$s", true, 301); exit;}
}
$producturl = "thank-you/";
CheckUrl ($producturl); //redirects the user if they are at the wrong place

Resources