Friday, April 28, 2017

Redirect to https but without .php

Leave a Comment

Now I have a https. I need a redirection in the .htaccess. I could find this:

RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

But I find that if the user writes:

http://myDomain/someFile 

It redirects to:

https://myDomain/someFile.php 

I suppose that the correct should be without .php How to do that?

Those are all the rules:

RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]  RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]  RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php 

5 Answers

Answers 1

Have your .htaccess as this by turning MultiViews off. Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.php.

You can also combine www and http rules into a single rule to avoid multiple 301 redirections.

Options -MultiViews RewriteEngine On  RewriteCond %{HTTP_HOST} !^www\. [NC,OR] RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]  RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+?)/?$ $1.php [L] 

Make sure to clear your browser cache before testing this change.

Answers 2

If you use php then you probably have Apache. You should find your .conf file. I have it at

/etc/apache2/sites-available/000-default.conf (Kubuntu 16.04.) 

and change the virtual host settings:

<VirtualHost *:80>     . . .      Redirect "/" "https://your_domain_or_IP/"      . . . </VirtualHost> 

Answers 3

Give a shot to this in your .htaccess (at the root of your public_html)

#FORCE HTTPS CONNECTION RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://yourDomain.com/$1 [R=301,L] #FORCE HTTPS CONNECTION 

Let me know if it worked out ;)

Answers 4

If you want to redirect http to https .. Use the following redirection code RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://yourDomain.com/$1 [R=301,L]

Answers 5

here is the working code for your problem. I have checked this on my server, and it is working fine

RewriteEngine on RewriteBase /  #for https RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]  # browser requests PHP RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]  # check to see if the request is for a PHP file: RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^/?(.*)$ /$1.php [L] 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment