0

Apologies if this seems pretty simple to some folk.

In the past I have set up 301 redirects with that snippet of code that is all over the internet:

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

However, a few things leave me uncertain for this particular project.

1) I'm using https, so there are four CNames now (is that (CName) correct terminology?) https://example.com https://www.example.com http://example.com http://www.example.com

2) I am using a CMS that needs the .htaccess file in order to search for CSS and other info when runnign the site. I'm therefore, as a less tech person, worried about fiddling with the .htaccess file (When this file was missing previously die to a separate issue the site was not functional)

Questions: 1) Does it matter where int he .htaccess file I place the redirect? Start/End of file? 2) How would I alter the snippet above to take into account the https?

Here is the .htaccess code

# Turn on URL rewriting only when mod rewrite is turn on

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Installation directory
    RewriteBase /

    # Protect application and system files from being viewed
    RewriteRule ^(application|modules|system|tests|sql) - [F,L]

    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Rewrite all other URLs to index.php/URL
    RewriteRule .* index.php?kohana_uri=$0 [L,QSA]
</IfModule>

# Protect the htaccess from being viewed
<Files .htaccess>
    order allow,deny
    deny from all
</Files>

# Don't show directory listings for URLs which map to a directory.
Options -Indexes

#Follow symlinks
Options +FollowSymlinks
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
Doug Fir
  • 19,971
  • 47
  • 169
  • 299

1 Answers1

1

To redirect all requests to http://example.com, you can prefix your rules with

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR]
RewriteCond %{HTTPS} =on
RewriteRule ^.*$ http://example.com/$0 [R,L]

This redirects all requests, which come with HTTPS or www.example.com or both.

When everything works as you expect, you can change R to R=301.

Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Thank you Olaf this is helpful. Just one question on that - will this be considered a 301 redirect? It's my understanding that that is important. – Doug Fir Apr 13 '13 at 13:15
  • OK thank you I'm glad to be aware of aggressive caching by browsers when using 301s. With that in mind I have tried your code and there appears there may be an issue: If you visit https ://example.com it does not redirect. All other variants DO redirect to example.com. Is example.com different from https ://example.com in the eyes of SEO? Thanks again for your help. If it helps the site can be seen here: http://tinyurl.com/c8djrvr – Doug Fir Apr 13 '13 at 13:21
  • @DougFirr I just tried this in my test environment and it redirects all three `http://www.example.com`, `https://www.example.com` and `https://example.com` to `http://example.com`. Do you have any error messages or something in the log files? – Olaf Dietsche Apr 13 '13 at 13:34
  • I believe that you see what you report! I do still seem to be having trouble with one of them though: https://example.com does not seem to redirect to example.com? – Doug Fir Apr 13 '13 at 14:16
  • @DougFirr What exactly happens? Does it stay on `https://example.com` or do you get any error message? – Olaf Dietsche Apr 13 '13 at 15:58
  • @DougFirr I just reread your comment above and realized, that you want to redirect everything to `https://example.com` and not to `http://example.com`. Is this correct? – Olaf Dietsche Apr 13 '13 at 16:01
  • no, I would like to redirect everything to http ://example.com So no s, no www :-) Thank you again for helping out here – Doug Fir Apr 13 '13 at 16:45