Force www and https - .htaccess

I'm trying to rewrite all urls to https://www. I've searched and tried various rules, but with no luck
I want example.com/something/something to be redirected to https://www.example.com/something/something
It's a wordpress installation. My .htaccess currently looks like this and it doesn't have the desired effect. Could you help me with the syntax?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=302,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
redirect 301 /tulossa-pian /
redirect 301 /vakava-2014 /
redirect 301 /omasivu /
redirect 302 /vakava-2015 /
redirect 302 /vakava-juliste /

Related

301 redirect adds an extra word to the end of the target url

I've tried a few work arounds but can't seem to find a solution that works.
When I try to create a 301 redirect for a URL, the redirect seems to work, but appends a 'location' word to the end of the target URL, which is referenced in the URL that's being redirected. For example:
Redirect 301 /web-design/ecommerce/london/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Results in:
https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/london/
There are other redirects in the file but I've also tried deleting them all incase they were having an impact.
Full htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Redirect 301 /web-design/ecommerce/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/reading/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/marlow/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/oxford/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/newbury/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/bracknell/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/wokingham/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Redirect 301 /web-design/ecommerce/london/ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/
Have every rule using mod_rewrite like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^web-design/ecommerce/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/reading/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/marlow/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/oxford/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/newbury/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/bracknell/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/wokingham/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteRule ^web-design/ecommerce/london/?$ https://www.orangegrovedesigns.co.uk/web-services/web-design/ecommerce-web-design/ [L,NC,R=301]
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Make sure to clear your browser cache completely before testing this change.

301 Redirect all pages of site to homepage

I have a client site who's business is closing. They want to keep their site up for now, but what they also want is that going to any previous URL, automatically redirects people to the homepage.
I've tried a number of ways to do it in .htaccess like the following:
RedirectMatch permanent .* http://www.hotskitchen.com
But nothing has worked. The only thing that happens is that the stylesheet goes away. What can I do to make this redirect work beyond writing lots of 301 redirects from individual pages?
Here's the complete .htaccess
# 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}/wordpress/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" -f
RewriteRule .* "/wordpress/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" [L]
</IfModule>
# END W3TC Page Cache core
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress
Redirect 301 /about http://www.hotskitchen.com/about-hots-kitchen/
Redirect 301 /about/our-concept http://www.hotskitchen.com/about-hots-kitchen/our-concept/
Redirect 301 /about/the-people http://www.hotskitchen.com/about-hots-kitchen/the-people/
Should do the trick for .htaccess:
RedirectMatch 301 ^/ http://www.hotskitchen.com/
Personally, I prefer mod_rewrite to achieve this when using Apache:
RewriteEngine On
Redirect 301 / http://www.hotskitchen.com/
Even better, a server listener in nginx for that specific domain name, which will work for any path:
server {
listen 80;
server_name www.oldwebsite.com;
return 301 http://www.hotskitchen.com/;
}

Basic 301 Redirect code not working

My pages are not redirecting for some reason. I'm trying to use the below code in my htaccess file.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
Options +FollowSymLinks
RedirectMatch 301 ^/([^-]+)-([^-]+)-([^-]+)-([^-]{2})\.html$ /$1-$2-in-$3-$4.html
RedirectMatch 301 ^/([^-]+)-([^-]+)-([^-]+)\.html$ /$1-$2-in-$3.html
# 301 --- http://www.domain.com/music-classes-san-diego-ca.html => http://www.domain.com/music-classes-in-san-diego-ca.html
RewriteRule ^music-classes-san-diego-ca\.html$ /music-classes-in-san-diego-ca.html? [L,R=301]
The 301 redirect is what I'm trying to get to work. It's not redirecting for me. I've also tried this:
redirect 301 music-classes-san-diego-ca.html http://www.domain.com/music-classes-in-san-diego-ca.html
Any time you are using wordpress, those rules always needs to go last because it routes everything to index.php. Try your rules before wordpress rules. FYI, I personally would use Rewrite for all 301's and not mix them with RedirectMatch.
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^music-classes-san-diego-ca\.html$ /music-classes-in-san-diego-ca.html? [L,R=301]
RedirectMatch 301 ^/([^-]+)-([^-]+)-([^-]+)-([^-]{2})\.html$ /$1-$2-in-$3-$4.html
RedirectMatch 301 ^/([^-]+)-([^-]+)-([^-]+)\.html$ /$1-$2-in-$3.html
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

HTACCESS redirect all old domain urls to new domain

I am new to HTACCESS and have a very specific question regarding 301 redirects. I am looking to redirect all .co.uk urls to .co but also I want to redirect all suburls to the root URL so for example I want
http://virtualfiles.co.uk/scenarios.html -> http://virtualfiles.co
A lot of these pages don't exist anymore so I would rather just relocate the user to the top level domain rather than a page that doesn't exist on the old or new domain.
I am set up on WordPress and the code I have so far is below. Any help would be amazing.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [NC]
You can try this in your domain1/.htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?virtualfiles\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://virtualfiles.co/$1 [R=302,L]
Or Try With RedirectMatch
RedirectMatch 301 ^/(.*)$ http://virtualfiles.co/$1

Subfolder query string htaccess redirect

Everything I can find for query string redirects talks about something like this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule .? http://mywebsite.com/? [L,R=301]
That's great, but I'm dealing with a url that looks like this:
example.com/portfolio/?gal=16
If I test it without the "portfolio/" it will work fine, but my old site is indexed as above. Any tips on how I can make it work with my URL structure?
EDIT Current .htaccess contents
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
# Redirects
Redirect 301 /feedback http://example.com/testimonials
Redirect 301 /about http://example.com/about-us
Redirect 301 /contact http://example.com/contact-us
#futile attempts at query string redirects omitted
Thanks.
New Edit to include #anubhava's suggestions
Might as well give you the actual .htaccess content since the site is now live.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^dev.bayshorephotography.com$ [OR]
RewriteRule (.*)$ http://bayshorephotography.com/$1 [R=301,L]
# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www.bayshorephotography.com$ [NC]
RewriteRule ^(.*)$ http://bayshorephotography.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^gal=16$
RewriteRule .? http://bayshorephotography.com/portfolio/weddings-02/? [L,R=301]
Redirect 301 /feedback http://bayshorephotography.com/testimonials
Redirect 301 /about http://bayshorephotography.com/about-us
Redirect 301 /contact http://bayshorephotography.com/contact-us
#301 Redirect Entire Directory to remove /blog/ from all blog pages except the main archive itself
RedirectMatch 301 /blog(/.*)/ $1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Old Redirects that don't work
#Redirect 301 /portfolio?gal=20 http://bayshorephotography.com/portfolio-items/weddings-04/
#Redirect 301 /portfolio?gal=29 http://bayshorephotography.com/portfolio-items/our-world-03/
#Redirect 301 /portfolio?gal=16 http://bayshorephotography.com/portfolio-items/weddings-02/
#Redirect 301 /portfolio?gal=18 http://bayshorephotography.com/portfolio-items/weddings-04/
#Redirect 301 /portfolio?gal=28 http://bayshorephotography.com/portfolio-items/album-samples-01/
#Redirect 301 /portfolio?gal=19 http://bayshorephotography.com/portfolio-items/weddings-05/
#Redirect 301 /portfolio?gal=22 http://bayshorephotography.com/portfolio-items/portraits-01/
#Redirect 301 /portfolio?gal=24 http://bayshorephotography.com/portfolio-items/our-world-01/
#Redirect 301 /portfolio?gal=17 http://bayshorephotography.com/portfolio-items/weddings-03/
#Redirect 301 /portfolio?gal=36 http://bayshorephotography.com/portfolio-items/portraits-04/
#Redirect 301 /portfolio?gal=35 http://bayshorephotography.com/portfolio-items/portraits-03/
#Redirect 301 /portfolio?gal=32 http://bayshorephotography.com/portfolio-items/weddings-01/
#Redirect 301 /portfolio?gal=25 http://bayshorephotography.com/portfolio-items/our-world-02/
#Redirect 301 /portfolio?gal=23 http://bayshorephotography.com/portfolio-items/portraits-02/
#Redirect gone /portfolio?gal=34
#Redirect gone /portfolio?gal=33
#Redirect gone /portfolio?gal=27
#Redirect gone /portfolio?gal=21
You need to change order of your rules and keep 301/302 before WP rules:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule .? http://example.com/? [L,R=301]
# Redirects
RewriteRule ^feedback/?$ http://example.com/testimonials [L,R=301]
RewriteRule ^about/?$ http://example.com/about-us [L,R=301]
RewriteRule ^contact/?$ http://example.com/contact-us [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Resources