htaccess redirect - advice needed

This section is for posting questions which are not directly related to WYSIWYG Web Builder.
Examples of off topics: web server configuration, hosting, programming related questions, third party scripts.

Note that these questions will generally not be answered by the administrators of this forum.
Post Reply
johnsmith0251
 
 
Posts: 269
Joined: Mon Aug 20, 2018 6:47 pm

htaccess redirect - advice needed

Post by johnsmith0251 »

As i went further into seo, i realized that people could assess my sites from multiple variants. So I decided to create a htaccess file to redirect everything from http://www, http://, and https:// to go to https://www.

I've noticed most large sites (apple, microsoft, google, facebook, etc) force to https and www.

I'm seeing a lot of different codes that seem to achieve the same thing.

This is what i'm currently using ...

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ "https\:\/\/www\.domain\.com\/$1" [R=301,L]

I've also seen it suggested this way:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>


Let me know your thoughts and what you use :)
johnsmith0251
 
 
Posts: 269
Joined: Mon Aug 20, 2018 6:47 pm

Re: htaccess redirect - advice needed

Post by johnsmith0251 »

i'm realizing that there are a lot you can add to the .htaccess - i'd be really interested in knowing what you add to yours!
User avatar
Rob
 
 
Posts: 205
Joined: Sun Jan 29, 2012 2:54 pm
Location: MN
Contact:

Re: htaccess redirect - advice needed

Post by Rob »

This is what I put in mine, and it seems to work pretty well for the most part....

rewriteengine on

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

rewritecond %{HTTP_HOST} ^www.mywebsite.com$ [OR]
rewritecond %{HTTP_HOST} ^mywebsite.com$
rewriterule ^index\.html$ "https\:\/\/www\.mywebsite\.com\/" [R=301,L]

rewritecond %{HTTP_HOST} ^mywebsite.com$
rewriterule ^(.*)$ "https\:\/\/www\.mywebsite\.com\/$1" [R=301,L]

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

RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]

ErrorDocument 404 /index.html

AddType 'text/html; charset=UTF-8' html

# DISABLE CACHING
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>

<IfModule mod_expires.c>
ExpiresActive On

# Images
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"

# Video
ExpiresByType video/mp4 "access plus 1 year"
ExpiresByType video/mpeg "access plus 1 year"

# CSS, JavaScript
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"

# Others
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
</IfModule>
The Website Guy - MN
Small Business Web Design
WWBman
 
 
Posts: 918
Joined: Fri Jan 08, 2010 6:10 pm

Re: htaccess redirect - advice needed

Post by WWBman »

@Rob, just curious, why do you direct 404 errors to /index.html rather than a separate error page?
johnsmith0251
 
 
Posts: 269
Joined: Mon Aug 20, 2018 6:47 pm

Re: htaccess redirect - advice needed

Post by johnsmith0251 »

you sparked two questions for me rob :)

This is the most intense I've seen for a redirect - i understand some of it, not all. Mind breaking down what this is doing?
Rob wrote: Thu Aug 15, 2024 12:20 am
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} mywebsite\.com [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=301,L]

rewritecond %{HTTP_HOST} ^www.mywebsite.com$ [OR]
rewritecond %{HTTP_HOST} ^mywebsite.com$
rewriterule ^index\.html$ "https\:\/\/www\.mywebsite\.com\/" [R=301,L]

rewritecond %{HTTP_HOST} ^mywebsite.com$
rewriterule ^(.*)$ "https\:\/\/www\.mywebsite\.com\/$1" [R=301,L]

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

RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
Are you doing this as a work around to bypass enable cacheing for seo purposes?
Rob wrote: Thu Aug 15, 2024 12:20 am
# DISABLE CACHING
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>

<IfModule mod_expires.c>
ExpiresActive On

# Images
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"

# Video
ExpiresByType video/mp4 "access plus 1 year"
ExpiresByType video/mpeg "access plus 1 year"

# CSS, JavaScript
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"

# Others
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
</IfModule>
User avatar
Rob
 
 
Posts: 205
Joined: Sun Jan 29, 2012 2:54 pm
Location: MN
Contact:

Re: htaccess redirect - advice needed

Post by Rob »

I actually don't use the cache portion of this file. Instead I add the following to the meta tags on the masterframe page... (seems to do well enough)

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

The ExpiresActive for images, video etc. is gzip compression per GTMetrix test.

The ip address redirect & lbwww (whatever that is) is per SEOSiteCheckup test.

The other redirects is to redirect http to https, non-www to www, and index.html to the root domain. The last one GWT doesn't seem to like (will send an error) but better for user experience I think.

If the pros want to chime in, that would be awesome.

I do think most hosts are implementing a way to do the above via their cpanel tools, and not all hosts like the format of the redirects in this file, but seems to work for most and does seem to pass the duplicate content test.

Rob
The Website Guy - MN
Small Business Web Design
johnsmith0251
 
 
Posts: 269
Joined: Mon Aug 20, 2018 6:47 pm

Re: htaccess redirect - advice needed

Post by johnsmith0251 »

I've spent hours researching this - everyone has their own code and opinions and there is deff multiple ways to achieve the same exact thing. Which for me sucks because it puts me in analysis paralysis.

When you say takes index.html to the root domain, is that just a way to remove the index.html part?

I ask, because even after my redirects - index.php keeps showing as a duplicate.
User avatar
Rob
 
 
Posts: 205
Joined: Sun Jan 29, 2012 2:54 pm
Location: MN
Contact:

Re: htaccess redirect - advice needed

Post by Rob »

Redirect index.html to the root domain so index.html does not show. Deals with duplicate content issues. Make sure the canonical tag matches the root url. Technically Google Webmaster Tools doesn't like this kind of redirect through.

I also redirect the 404 to the root url for UX purposes. I worry about a user being turned off with a page not found and clicking off right away. Maybe I am not thinking about it right. Technically SEO says you're supposed to have a 404.html page, just don't let Google index it.
The Website Guy - MN
Small Business Web Design
Post Reply