DizzyDan

www redirect issue

7 posts in this topic

I am using WordPress and trying to redirect to www.

 

I added the line below to my htaccess file and the redirect works but it send the URL here: http://www.creativejuicesmusic.com/wp-content/cache/page_enhanced/creativejuicesmusic.com/_index.html_gzip

RewriteCond %{HTTP_HOST} ^creativejuicesmusic.com [nocase]
RewriteRule ^(.*) http://www.creativejuicesmusic.com/$1 [last,redirect=301]

This is my full htaccess file

# BEGIN W3TC Browser Cache
<IfModule mod_deflate.c>
    <IfModule mod_filter.c>
        AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/bmp application/java application/msword application/vnd.ms-fontobject application/x-msdownload image/x-icon image/webp application/json application/vnd.ms-access application/vnd.ms-project application/x-font-otf application/vnd.ms-opentype application/vnd.oasis.opendocument.database application/vnd.oasis.opendocument.chart application/vnd.oasis.opendocument.formula application/vnd.oasis.opendocument.graphics application/vnd.oasis.opendocument.presentation application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.text audio/ogg application/pdf application/vnd.ms-powerpoint image/svg+xml application/x-shockwave-flash image/tiff application/x-font-ttf application/vnd.ms-opentype audio/wav application/vnd.ms-write application/font-woff application/font-woff2 application/vnd.ms-excel
    <IfModule mod_mime.c>
        # DEFLATE by extension
        AddOutputFilter DEFLATE js css htm html xml
    </IfModule>
    </IfModule>
</IfModule>
<FilesMatch "\.(html|htm|rtf|rtx|svg|txt|xsd|xsl|xml|HTML|HTM|RTF|RTX|SVG|TXT|XSD|XSL|XML)$">
    <IfModule mod_headers.c>
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</FilesMatch>
<FilesMatch "\.(bmp|class|doc|docx|eot|exe|ico|webp|json|mdb|mpp|otf|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|pot|pps|ppt|pptx|svg|svgz|swf|tif|tiff|ttf|ttc|_ttf|wav|wri|woff|woff2|xla|xls|xlsx|xlt|xlw|BMP|CLASS|DOC|DOCX|EOT|EXE|ICO|WEBP|JSON|MDB|MPP|OTF|_OTF|ODB|ODC|ODF|ODG|ODP|ODS|ODT|OGG|PDF|POT|PPS|PPT|PPTX|SVG|SVGZ|SWF|TIF|TIFF|TTF|TTC|_TTF|WAV|WRI|WOFF|WOFF2|XLA|XLS|XLSX|XLT|XLW)$">
    <IfModule mod_headers.c>
         Header unset Last-Modified
    </IfModule>
</FilesMatch>
# END W3TC Browser Cache

# BEGIN W3TC Page Cache core
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteRule .* - [E=W3TC_ENC:_gzip]
    RewriteCond %{HTTP_COOKIE} w3tc_preview [NC]
    RewriteRule .* - [E=W3TC_PREVIEW:_preview]
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} =""
    RewriteCond %{REQUEST_URI} \/$
    RewriteCond %{HTTP_COOKIE} !(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle) [NC]
    RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" -f
    RewriteRule .* "/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" [L]
</IfModule>
# END W3TC Page Cache core

# EXPIRES CACHING #
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 1 week"
</IfModule>
# EXPIRES CACHING #

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^creativejuicesmusic.com/
RewriteRule ^(.*)$ http://www.creativejuicesmusic.com/$1 [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Share this post


Link to post
Share on other sites

one way is

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

another is

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

Just google none www to www htaccess mod rewrite, you should get loads of examples of how to do this, and various example to manipulate the url to your requirements.

Share this post


Link to post
Share on other sites

This code works for the www but for some reason when this code is applied and you enter creativejuicesmusic.com int the web browser it takes you to: http://www.creativejuicesmusic.com/wp-content/cache/page_enhanced/creativejuicesmusic.com/_index.html_gzip instead of http://www.creativejuicesmusic.com

 

So something funky is going on.

 

I moved the gzip code to go after the wordress code and everything seems to check out.

Share this post


Link to post
Share on other sites

You're having it do all of the other URL rewriting before redirecting. If the host is not correct then you should do that redirect first, before all of the other Wordpress stuff you have in that file.

Share this post


Link to post
Share on other sites

OK so I placed this at the very top of the htaccess file

# Force WWW
RewriteCond %{HTTP_HOST} !^www\.creativejuicesmusic\.com [NC]
RewriteRule (.*) http://www.creativejuicesmusic.com/$1 [R=301,L]

# Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

But now in google page speed insights it throws this at me:

 

 

 

Avoid landing page redirects
Your page has 2 redirects. Redirects introduce additional delays before the page can be loaded.

 

 

 

I thought doing this via htaccess was the correct kind of fix for this.

Share this post


Link to post
Share on other sites

It doesn't really matter where you specify a redirect, it's still a redirect.

 

If you're going to force HTTPS, then instead of redirecting them to HTTP so that you can then redirect them again to HTTPS, just redirect them to HTTPS the first time.

Share this post


Link to post
Share on other sites

Ahh that worked, forgot about that sneaky 's'

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

Thanks for all the help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now