rewrite htaccess to https with route - php

Im using the following .htaccess for my website. I redirect all urls to index.php if their isn't a file located at the url.
Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
This way http://website.com/user/user_name/ goes to index.php
and
http://website.com/css/style.css gives the actual css file (if style.css exists).
This works great, only i want to force https:// on the urls and i can't manage to get it to work. Any help is greatly appreciated.
Edit:
Using Jeroen's answer I got it working with the following .htaccess
Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

As you are using CloudFlare's ssl - I assume Flexible SSL - you need a different solution although #Fox's solution is correct for a "normal" ssl connection.
For CloudFlare you need something like:
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,QSA]
Also see CloudFlare's support article.

Try this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

Related

Apache Redirect all to index.php with https

I have read many answers on the subject but none is mentionning how to combine :
redirecting all traffic to index.php + redirect all http to httpS
the following works great for redirecting all to index.php :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|robots\.txt)
RewriteRule ^(.*) index.php/params=$1 [L,QSA]
before I was doing this I was able to force http to https using this :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
I just cannot find a way to mix both, so that It would redirect all traffic to index.php using the same conditions and force https.
* edit *
in case somebody else gets confused like I did.
The reason why https was systematically returning a 404 when I was calling rewritten urls is because... My domain configuration file was incomplete, my website-ssl.conf was missing the AllowOverride All directive, this is why it was all working fine until I added the url rewriting. My non-ssl was correctly setup for url-rewriting so this is why it took me a little while to realise this was not working when https.
I have then added the necessary in my /etc/apache2website-ssl.conf
<Directory /var/www/vhosts/exemple.com>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
and this in my .htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://exemple.com/$1 [R,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|robots\.txt)
RewriteRule ^(.*) index.php/params=$1 [L,QSA]
ErrorDocument 404 /index.php
I hope that'll help somebody.
Thank you for your help
Just keep the https redirection rule above other rules :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|robots\.txt)
RewriteRule ^(.*) index.php/params=$1 [L,QSA]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/index.php
</IfModule>
So in the above case, if user is going to http link, both(re-write to https and forward to index.php) will happen (i tried and its working). but if user is going to https directly, then you should have simple rewriting to index.php in your default-ssl

htaccess wordpress https rewrite rule

I am hoping that you can help me with the htaccess rewrite rule below.
# BEGIN WordPress
# WPhtc: Begin Custom htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I have recently changed my wordpress site from http to https... The problem is that old url's redirect to the domain name instead of the https version of that page
eg
if I access the following page https://domain.com/test/testing/ it works 100%, now if I change the https part to http then the page redirects to https://domain.com instead of to https://domain.com/test/testing/ how do I fix it so that if you go to the old version page http://domain.com/test/testing/ (the not https version) that it redirects to https://domain.com/test/testing/ instead of just the domain name https://domain.com
You have to find a workaround for %{REQUEST_FILENAME} since this only represents the file that is accessed. But you obviously want to access the SSL vHost.
So you might hardcode the https into your .htaccess.
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
This might help you alot. (found the code above there)
I've been struggling also with this issue and finally I found a solution for the home redirection and the wordpress in the same htaccess file, and finally it also works for old http links, redirecting to https:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
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
I tried your solution. It worked well, but if you do that you'll need to manually change all your internal links.
This works better ;)
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
I use this .htaccess for wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

.htaccess HTTPS to HTTP Not working

I am trying to redirect visits to https://www.domain.co.uk to http://www.domain.co.uk. I have researched this, but have come to a dead end.
Many people including people here, on SO suggest to use:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This is my entire .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I have tried putting the https rewrite code above, below and within the <IfModule mod_rewrite.c>. But had no luck.
I know the .htaccess is being read, as I can make it return a 500 internal server error when using random letters.
Thanks for your help.
EDIT
Anubhava found out my server isn't listening for port 443 as my VirtualHost has no entry for it.
Here's that section of my httpd.conf
If it is WP then in your permalink settings change your site's URL to http://domain.com which seems to be https://domain.com at present. WP forces that URL if you're using a different one.
Your full .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Apache mod rewrite for dynamic pages

I'm having a problem with my mod rewrite rule that I wrote for my website, nothing seems to change as my pages URL are loading the same as before, if anyone could have a look at it and let me know if there is any problems it would be very much appreciated, thanks!
REWRITE RULE
RewriteEngine On
RewriteRule ^([^/]*)/$ /index.php?art_id=$1 [L]
URL
http://www.test.com/index.php?art_slug=test
DESIRED RESULT
http://www.test.com/test
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# to externally redirect from /index.php?art_slug=test to /art_slug/test
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+/(?:index\.php|)\?([^=]+)=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
# to internally forward from /art_slug/test to /index.php?art_slug=test
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/(.+)$ /index.php?$1=$2 [L,QSA]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php?art_id=$1 [L]
Should work. :)
RewriteEngine On
RewriteRule (.*)/$ search.php?keyword=$1
Should work. :)

Improving .htaccess with SSL and WWW on Codeigniter

I am trying to make SSL and Redirection work perfectly with my web application. I wish to achieve that always https://www.mydomain.com/ should be loaded in the browser - although, if he types a subdomain - it should be redirected to https://subdomain.mydomain.com/ instead.
I mean to say, everything should be SSL - here is what I am doing currently
Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www) [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule (.*)\.php$ index.php/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
Also, I wish to improve this .htaccess file so to introduce more security measures plus also allow only js, css and images files to be accessible by everyone - rest everything hidden or redirected to a 404 page.
Please guide, I would be greatly thankful!
These rewrite conditions may help you, I use this to make my CodeIgniter go to HTTPS:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]

Categories