I use codeigniter with some URL routing.
I want to migrate from http to https.
This is my current .htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^eternalcitytours\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^XXX\.XXX\.XXX\.XXX [NC]
RewriteRule ^(.*)$ http://eternalcitytours.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
# Development
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|scripts|styles|vendor|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
The above works fine on http.
To go to https i changed the .htaccess to be:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://eternalcitytours.com/%{REQUEST_URI} [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^eternalcitytours\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^037\.061\.237\.216 [NC]
RewriteRule ^(.*)$ https://eternalcitytours.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
# Development
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|scripts|styles|vendor|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
However, the above code doesnt work as I get 2 problems:
whenever incoming traffic comes to me from an http link, the resultant url has an extra / in it: e.g. https://eternalcitytours.com//en/5/About-Eternal-City-Tours instead of https://eternalcitytours.com/en/5/About-Eternal-City-Tours
If someone has https://WWW.eternalcitytours.com then the page wont load and an error is given. (ERR_SSL_PROTOCOL_ERROR)
Can someone help with my .htaccess code as i honestly dont know what i'm doing.
thanks
You need to use this rule for http -> https:
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://eternalcitytours.com%{REQUEST_URI} [R=301,NE,L]
Note / removed before %{REQUEST_URI}.
Make sure to clear browser cache before testing it.
For problem #2 it seems your SSL cert is for eternalcitytours.com domain only. It is because SSL handshake hapens before mod_rewrite rules are executed and there is no rule based fix except that you keep https links with www in your HTML source.
Related
I've made changes to my htaccess file on my WordPress site to redirect traffic from http to https.
Most cases it works fine and redirects traffic to https, but some cases it doesn't.
For example, if I try access home page with http in address it redirects to https, but if I try and access another page on the site with http in URL it stays on http:
http: //example.com redirects fine to https: //example.com
http: //example.com/page stays on http: //example.com/page
Current htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
</IfModule>
# END WordPress
I've tried numerous answers in other question, what else can I try?
try this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Why do you have the following at the bottom? What happens when you remove this? I think this may be conflicting with RewriteCond %{HTTPS} off at the top. What happens when you remove this...
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
It will work for www and https
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Change your site address in Settings:
and add the code below at the top of WordPress’ .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC]
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301,NC]
I had the same problem, and the fix was really simple... Just be sure to put the https redirection BEFORE the original rewrite code of Wordpress...
Exemple :
# Redirection code (redirect no www to www and http to https)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I am using codeigniter framework,set .htaccess file to remove index.php and trying to enable HTTPS protocol for the server, and something happened.
HTTP:
Everything is okay, when I access http://www.example.com/controller/method or http://www.example.com/index.php/controller/method
HTTPS:
Everything is okay, when I access https://www.example.com/index.php/controller/method
Got 404 not found when I access https://www.example.com/controller/method
I think that is .htaccess file problem, it is look like htaccess file not working for HTTPS protocol.
.htaccess file of my site.
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
Is something wrong? thanks a lot.
Note: The SSL rewrite should happen before the index.php rewrite in your .htaccess file.
So, Don't do this setting (as we don't change anything in server apache config file.
)
AllowOverride All
just apply the Setting 2 it works.
I have faced the same issue and resolved it by adding
AllowOverride All
Setting 1:
FYI we have 2 config files.
Default Apache config (which runs with http)
SSL config (which runs with https)
Go to SSL config and add
<Directory "/var/www/html">
AllowOverride All
</Directory>
**Setting 2:**The .htaccess file should be like this...
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
It worked for me.
First you remove $config['base_url] from your config file and put ''(blank) in your $config['index'] attribute ..
please set your .htaccess file like..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
Try this
I used this in one of my project. May its helps you
#AuthType Basic
#AuthName "restricted area"
#AuthUserFile /home/site_name/public_html/.htpasswd
#require valid-user
RewriteEngine On
#RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.site_name.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?.site_name.lk$ [NC]
RewriteRule ^(.*)$ https://www.site_name.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?site_name\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? https://www.%1site_name.com%{REQUEST_URI} [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond $1 !^(index\.php|assets|files|robots\.txt)
# RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Note site_name in the above htaccess should replaced to your website name
you can try this, it works for my project.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
reference: https://wiki.apache.org/httpd/RewriteHTTPToHTTPS
if you want to http->https AND remove www AND remove index.php here's the solution:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
I have CodeIgniter setup in a sub-directory of a sub-domain. My htaccess file works for the following: http://subdomain.domain.com/subdirectory/
The working htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /subdirectory/
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php?/$1 [L]
However this does not work when I point a domain name http://www.mydomainname.org to public_html/subdirectory.
What I'm attempting to achieve is I'd like to use http://www.mydomainname.org/whatever/ to mask http://sub.domain.com/subdirectory/index.php/whatever
Is this possible? Is there anything I can add to my htaccess that will allow me to achieve this?
I attempted to use the htaccess answer here, but had no luck.
I think if you want both to work, you'll need to add extra conditions for the hostname, try something like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?mydomainname\.com$ [NC]
RewriteRule ^$ /index.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomainname\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php?/$1 [L]
# for subdomain
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule ^$ /subdirectory/index.php [L]
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(?:subdirectory/|)(.*)$ /subdirectory/index.php?/$1 [L]
I'm trying to get my htaccess rewrite rules to remove the index.php from the url AND also redirect the www. requests to the non-www version.
This is my htaccess which works fine with removing the index.php:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
And I came across another question on how to remove the www part:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
But I just cant seem to get them to play nicely together! Any advice/suggestions most appreciated!
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteEngine On "starts" rewriting. RewriteCond and RewriteRule work as pairs.
Here, we send user to non-www version first, and cleans URLs.
You first need to make sure that the "non-www" rewrite rule comes first and then the one that will redirect all requests to the CodeIgniter bootstrap file. Do note that the following code will honor only HTTP rewrites. If you also need HTTPS it requires some modifications.
RewriteEngine on
# Sending all www requests to the non-www version.
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
# Now the CodeIgniter part
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I'm using my htaccess file with mod_rewrite to create clean urls like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
I would also like to force the site to have the 'www' subdomain and most importunately add a trailing slash if the url doesn't have one.
I am an absolute noob with mod_rewrite and I've tried accomplishing this on my own by combining other code I found on google (sad I know), but I always end up with a 500 error.
Here's the code I found for force www:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
</IfModule>
Thanks for your help.
Try separating out the www and the trailing slash check. This is tested and hopefully working for you. You didn't say if you're running placing at domain root or in a subdirectory - usually good info when asking for help with htaccess.
RewriteEngine On
# Assuming you're running at domain root. Change to working directory if needed.
RewriteBase /
#
# www check
# If you're running in a subdirectory, then you'll need to add that in
# to the redirected url (http://www.mydomain.com/subdirectory/$1
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
#
# Trailing slash check
# Don't fix direct file links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
#
# Finally, forward everything to your front-controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [QSA,L]
To debug, comment out the individual sections and see what is/isn't working.
Use this and forgot your problems ;)
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*?)/*$ http://%1/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://your-domain.ru/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>