.htaccess RewriteCond for a specific url path without https - .htaccess

there is a problem with the htaccess rewrite on a specified url (service/scripts/) path. I dont like to redirect if the path contains "service/scripts and just open the link with http". All other links should redirect to https. I m using the Codeigniter Framework. And i have tried the following code TRY1 and TRY2:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]
RewriteCond $1 !^(index\.php|(.*)\.swf|assets|service/*|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Header add Access-Control-Allow-Origin "https://url.info/"
<IfModule mod_rewrite.c>
# TURN SSL ON
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.url.info/$1 [R,L]
# TRY 1
#RewriteCond %{HTTPS} on
#RewriteCond %{REQUEST_URI} ^/service.*
#RewriteRule ^(.*)$ http://www.url.info/$1 [R=301,L]
#RewriteCond %{HTTPS} off
#RewriteCond %{REQUEST_URI} !^/service.*
#RewriteRule ^(.*)$ https://www.url.info/$1 [R=301,L]
# TRY 2
#RewriteCond %{HTTPS} on
#RewriteCond %{REQUEST_URI} ^/service/scripts/?.*$
#RewriteRule ^(.*)$ http://www.url.info/$1 [R=301,L]
#RewriteCond %{HTTPS} off
#RewriteCond %{REQUEST_URI} !^/service/scripts/?.*$
#RewriteRule ^(.*)$ https://www.url.info/$1 [R=301,L]
# Removes index.php RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
Its always redirecting like the and the RewriteCond %{REQUEST_URI} !^/service.* is not triggert or the secound RewriteCond is overriding it...
Please can somebody help me?
Best regards,
Dave

Now I used a PHP Redirect in the HTML Head on all pages to solve the Problem.
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
if(!headers_sent()) {
header("Status: 301 Moved Permanently");
header(sprintf(
'Location: https://%s%s',
$_SERVER['HTTP_HOST'],
$_SERVER['REQUEST_URI']
));
exit();
}
}

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]

www to non-www with SSL without index.php

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]

Removing index.php from subdomain

I have the following .htaccess file in my subdomain.
My website has http://hello/forum/index.php, and would like to hide this index.php file, and make it http://hello/forum.
Whenever I use the attached .htaccess, it directs to http://hello, not http://hello/forum. Am I missing something?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) forum/$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ forum/index.php/$1 [L]
</IfModule>
Try these rules:
RewriteCond %{THE_REQUEST} ^GET\ /(forum)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /forum/index.php/$1 [L]
I was able to solve this issue by adding
DirectoryIndex index.php index.html
in .htaccess file.

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

Trying to redirect directory to https but getting double URL segment on return

I'm trying to redirect one directory on my site to https. The directory is not really a directory at all because the site's dynamic. I'm using ExpressionEngine as the CMS.
I already have a few lines in my .htaccess file to remove the index.php ExpressionEngine adds to all of its urls.
Here's what I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} store
RewriteRule ^(.*)$ https://mysite.com/store/$1 [R,L]
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
When I visit, http://mysite.com/store/, it returns https://mysite.com/store/store/.
I think it might have to do with the following:
RewriteRule ^(.*)$ /index.php?/$1 [L]
The above line has a question mark after the index.php because I was getting a "No input file" error, and that was the recommended fix.
Any thoughts?
This line here:
RewriteCond %{REQUEST_URI} store
Should have a ! in front of "store":
RewriteCond %{REQUEST_URI} !store
because you don't want it to redirect if the URI is already /store.
Edited the line:
RewriteRule ^(.*)$ https://mysite.com/store/$1 [R,L]
to read:
RewriteRule ^(.*)$ https://mysite.com/$1 [R,L]
Now it works. Here's the full code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} store
RewriteRule ^(.*)$ https://mysite.com/$1 [R,L]
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>

Resources