Basic 301 Redirect code not working - .htaccess

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

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/;
}

Force www and https

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 /

Redirecting 404s using 301 redirects with .htaccess

The .htaccess file I'm editing contains the following code for redirecting http://www.example.com to http://example.com:
AddHandler fcgid54-script .php
# 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
Without writing a new .htaccess file from scratch, I'm looking to redirect a few other pages that are returning 404s. I tried the following but it didn't work:
AddHandler fcgid54-script .php
# 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>
301 Redirect /blank/blah/page/ http://example.org/blank/blah/
# END WordPress
I've also tried a bunch of other other iterations, but nothing is working so far.
Don't mix mod_rewrite rules with mod_alias ones and keep 301 before internal WP rule:
AddHandler fcgid54-script .php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^blank/blah/page/?$ http://example.org/blank/blah/ [L,NC,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
try this it will work :
ErrorDocument 404 http://www.example.com/abc.html
By adding this line in htaccess file, pages that are returning 404s are forwarded to the specified url. such as http://www.example.com/abc.html

htaccess redirection subfolder

Hi I am looking to the following in .htaccess:
RewriteRule ^pqr$ /pqr.php
[without 301 redirect, because I want the user to see "pqr" in the broswer and not "pqr.php"]
The problem is that when I try the above .htaccess code, /pqr automatically redirects to the "pqr" subdirectory (/pqr/). I'm looking for a way to stop that automatic redirection.
Do you know how to do that with htaccess?
my current htaccess code
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Wordpress_Work/placewise/wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /Wordpress_Work/placewise/wordpress/index.php [L]
</IfModule>
# END WordPress
That should do it:
RewriteEngine on
RewriteRule ^/([^/\.]+)/?$ $1.php [L]

Resources