How to fix htaccess ssl https redirect? - php

I have used search and Google but can't seem to find a solution. I am trying to redirect it to HTTPS but am having some issues. I keep getting:
ERR_TOO_MANY_REDIRECTS
Below is my htaccess file. I would prefer that instead of adding a code to each html code. I have tried everything and just cant figure it out.
# Multiple Environment config
# Set this to development, staging or production
# SetEnv PYRO_ENV production
<IfModule mod_rewrite.c>
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
# disable the Apache MultiViews directive if it is enabled on the server. It plays havoc with URL rewriting
Options -MultiViews
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RedirectMatch 403 ^/.*/(system/cms/cache|system/codeigniter|system/cms/config|system/cms/logs|\.git|\.hg).*$
# Send request via index.php (again, not if its a real file or folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
</IfModule>
Edit: Problem solved Below code Worked. Please Lock
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://mydomain(dot)com/$1 [R,L]

try below code, it should work:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -Indexes
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RedirectMatch 403 ^/.*/(system/cms/cache|system/codeigniter|system/cms/config|system/cms/logs|\.git|\.hg).*$
# Send request via index.php (again, not if its a real file or folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Related

I need laravel website to open in https without public in url, how can I do it?

I have created website on Laravel, so the website was being open under www.domain.com/public/. So I searched my answer here and found out this was working for me, after I added this code the page was opening at www.domain.com without public added to the url:
htaccess:
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
But now I installed SSL and I want to force it to open a page at https link but I dont know why it won't. I setted my APP URL inside .evn file to https link and added this code to the htacces file:
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ https://domain/$1 [L]
</IfModule>
And it now opens webpage every time in https but with additional public in url, when I try to delete public and set code to be:
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ https://domain/$1 [L]
</IfModule>
It wont open my website at all. Please if anyone knows a fix to this I will be so much thankfull, best wishes!
How about this one?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

.htaccess can't add directory slash

So I've been trying to rewrite my php URLs with .htaccess so that they are more SEO/user friendly. Here is my code:
RewriteBase /
Options +FollowSymLinks -MultiViews
DirectorySlash On
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*)/$ /?lang=$1 [L]
RewriteRule ^([^/]*)/contact$ /contact?lang=$1 [L]
What this does is, it turns this:
http://www.example.com/?lang=en
http://www.example.com/contact?lang=en
Into this:
http://www.example.com/en/
http://www.example.com/en/contact
The problem:
http://www.example.com/en/
is not the same thing as:
http://www.example.com/en
And I get a 404 Not Found error when I try to point my browser's address to http://www.example.com/en. How can I make both versions of this work?
Adding DirectorySlashes On to my root .htaccess did not work. But my mobile version located in the /m/ folder has the following .htaccess:
# Set dir index
DirectoryIndex index.php
# Set dir slashes
DirectorySlash On
And it works! When I visit http://www.example.com/m I get redirected to http://www.example.com/m/. That is the wanted behavior. I guess it works because /m/ is a sub directory...
The question:
How can I make http://www.example.com/en redirect to http://www.example.com/en/ without breaking the rest of my URLs?
Edit (my entire .htaccess):
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
# Disable directory views
Options All -Indexes
IndexIgnore *
# Follow Symbolic links
Options +FollowSymLinks -MultiViews
# Create Directory Slashes
DirectorySlash On
# Rewrite urls
<IfModule mod_rewrite.c>
RewriteEngine on
# Pretty desktop URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*)/$ /?lang=$1 [L]
RewriteRule ^([^/]*)/prices$ /prices?lang=$1 [L]
RewriteRule ^([^/]*)/offers$ /offers?lang=$1 [L]
RewriteRule ^([^/]*)/maps$ /maps?lang=$1 [L]
RewriteRule ^([^/]*)/contact$ /contact?lang=$1 [L]
RewriteRule ^([^/]*)/links$ /links?lang=$1 [L]
RewriteRule ^([^/]*)/error$ /error?lang=$1 [L]
RewriteRule ^([^/]*)/verify-new$ /verify-new?lang=$1 [L]
RewriteRule ^([^/]*)/verify-old$ /verify-old?lang=$1 [L]
RewriteRule ^en/ski-rent-prices$ /ski-rent-prices [L]
# Rewrite mobile URLs
RewriteRule ^([^/]*)/m/$ /m/?lang=$1 [L]
# WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Rewrite URL without file extensions
# For .PHP files
RewriteRule ^error$ error.php [L]
RewriteRule ^offers$ offers.php [L]
RewriteRule ^prices$ prices.php [L]
RewriteRule ^maps$ maps.php [L]
RewriteRule ^contact$ contact.php [L]
RewriteRule ^links$ links.php [L]
RewriteRule ^verify-new$ verify-new.php [L]
RewriteRule ^verify-old$ verify-old.php [L]
# For .PDF files
RewriteRule ^ski-rent-prices$ ski-rent-prices.pdf [L]
</IfModule>
# Add Custom Error pages
ErrorDocument 400 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
# Default Character set
IndexOptions Charset=UTF-8
AddDefaultCharset UTF-8
# Hide the server signature
ServerSignature Off
<IfModule mod_rewrite.c>
# Add correct content types for documents
# JS:
AddType text/javascript .js
# CSS:
AddType text/css .js
# TXT:
AddType text/plain .txt
</IfModule>
<IfModule mod_headers.c>
# IE Compability Mode
BrowserMatch (MSIE|Trident) ie
Header set X-UA-Compatible "IE=Edge,chrome=1" env=ie
# Disallow 3rd party iframes
Header always append X-Frame-Options SAMEORIGIN
</IfModule>
Rewrite --- www.example.com/?lang=en => www.example.com/en/
RewriteCond %{QUERY_STRING} (^|&)lang=en($|&)
RewriteRule ^$ /en/? [L,R]
Updated code for testing
RewriteBase /
Options +FollowSymLinks -MultiViews
DirectorySlash On
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/$
RewriteRule ^(.+)/ ?lang=$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/m/$
RewriteRule ^(.+)/(.+)/ $2?lang=$1 [L,R=301]
RewriteRule ^(.+)/(.+)/m/ $2/m/?lang=$1 [L,R=301]
See the browser address bar and check if the redirections are correct. If it wrong provide the source link and redirected link. If it is ok, Then use the below code.
RewriteBase /
Options +FollowSymLinks -MultiViews
DirectorySlash On
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/$
RewriteRule ^(.+)/ ?lang=$1 [L]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/m/$
RewriteRule ^(.+)/(.+)/ $2?lang=$1 [L]
RewriteRule ^(.+)/(.+)/m/ $2/m/?lang=$1 [L]
I have tried the code with the below links and its working fine.
http://www.example.com/en
http://www.example.com/en/
http://www.example.com/en/contact
http://www.example.com/en/contact/
http://www.example.com/en/contact/m
http://www.example.com/en/contact/m/
http://www.example.com/en/prices
http://www.example.com/en/prices/
http://www.example.com/en/prices/m
http://www.example.com/en/prices/m/
Check these links yourself and also other links as well.

Laravel Shared Hosting .htaccess

I am trying to deploy a Laravel project onto a share hosting, I've managed to get most of the hard work done but I cannot strip off the /public directory without a Forbidden issue.
The website works and shows same pages for these links
www.mywebsite.com/test/index.php
www.mywebsite.com/test/public/
But without the /index.php It returns ->
Forbidden
You don't have permission to access /test/ on this server.
Currently my .htaccess looks like the following.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ public/index.php [L]
</IfModule>
Any ideas?
this is the magic script i use (add to the .htaccess in public_html)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Try this rule in test/.htaccess:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteRule ^$ public/index.php [L]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ public/index.php [L]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
</IfModule>
This script is working for me.
<IfModule mod_rewrite.c>
RewriteEngine On
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove public folder form URL
RewriteRule ^(.*)$ public/$1 [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>

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