Forum Moderators: phranque

Message Too Old, No Replies

Help for htacess 301

hacess

         

MrCranium

9:31 pm on Apr 20, 2016 (gmt 0)

10+ Year Member



Hi friends!

I have the following .htacess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#canonical urls
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

#trailing slashes
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_URI} !\.php$
RewriteCond %{REQUEST_URI} !\.html$
RewriteCond %{REQUEST_URI} !\.htm$
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteCond %{REQUEST_URI} !\.gif$
RewriteCond %{REQUEST_URI} !\.png$
RewriteCond %{REQUEST_URI} !\.shtml$
#sitemap
RewriteCond %{REQUEST_URI} !\.xml$
#RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) $1/ [R=301,L]
#RewriteRule ^(.*)([^/])$ https://%{HTTP_HOST}/$1$2/ [L,R=301]
#double trailing slashes
#RewriteCond %{REQUEST_URI} ^([^/]+/)([^/]+/)/$
#RewriteCond %{REQUEST_URI} !\.php$
#RewriteCond %{REQUEST_URI} !\.html$
#RewriteCond %{REQUEST_URI} !\.htm$
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule (.*) $1/ [R=301,L]
</IfModule>


I put an SSL certificate on the site and this .htaccess Google understands redirection as 302 and I want a 301 redirect .

The following code redirects as 301 but no force www in https :

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

#trailing slashes
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_URI} !\.php$
RewriteCond %{REQUEST_URI} !\.html$
RewriteCond %{REQUEST_URI} !\.htm$
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteCond %{REQUEST_URI} !\.gif$
RewriteCond %{REQUEST_URI} !\.png$
RewriteCond %{REQUEST_URI} !\.shtml$
#sitemap
RewriteCond %{REQUEST_URI} !\.xml$
#RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) $1/ [R=301,L]
#RewriteRule ^(.*)([^/])$ https://%{HTTP_HOST}/$1$2/ [L,R=301]
#double trailing slashes
#RewriteCond %{REQUEST_URI} ^([^/]+/)([^/]+/)/$
#RewriteCond %{REQUEST_URI} !\.php$
#RewriteCond %{REQUEST_URI} !\.html$
#RewriteCond %{REQUEST_URI} !\.htm$
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule (.*) $1/ [R=301,L]
</IfModule>


What can I do to force https and www without losing the 301 redirect ?

[edited by: engine at 9:46 am (utc) on Apr 21, 2016]
[edit reason] please use example.com [/edit]

Andy Langton

11:23 am on Apr 30, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is there a reason you removed the www rule?

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]


Presumably, this will work if you just add it back in after your HTTPS redriect?

not2easy

3:52 pm on Apr 30, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You can combine the two rules (for www and https) as explained here: [webmasterworld.com...]
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [OR]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.example.com/$1 [R=301,L]