Welcome to WebmasterWorld Guest from 54.156.58.187

Forum Moderators: Ocean10000 & incrediBILL & phranque

Having issues with 301 redirect I created not working

I've added the 301 lines of code required, but only the homepage redirects

     
6:19 pm on Aug 17, 2017 (gmt 0)

New User

joined:Aug 17, 2017
posts: 10
votes: 0


Hello,

I have put together this code in the .htaccess file to redirect certain pages from an existing ".com" domain hosted on WordPress to a new ".co.uk" domain hosted on Shopify.

RewriteEngine on
Redirect 301 / https://www.example.co.uk/
Redirect 301 /speciality-tea.html https://www.example.co.uk/collections/all
Redirect 301 /ginseng-ginger-lime-tea.html https://www.example.co.uk/collections/all
Redirect 301 /earl-grey-tea.html https://www.example.co.uk/collections/all
Redirect 301 /tea-ceremony.html https://www.example.co.uk/blogs/news

However, only the homepage redirect is currently working. These are the URLs I'm trying to redirect to the above URLS that are not working:

http://www.example.com/speciality-tea/
http://www.example.com/product/ginseng-ginger-lime-tea/
http://www.example.com/product/earl-grey-tea/
http://www.example.com/tea-ceremony/

I'm not 100% sure where I'm going wrong here or what I'm missing. Thanks for your help in advance.

[edited by: phranque at 11:54 pm (utc) on Aug 17, 2017]
[edit reason] [webmasterworld.com...] [/edit]

12:53 am on Aug 18, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10989
votes: 84


welcome to WebmasterWorld, BKPirate! [webmasterworld.com]

Redirect 301 /speciality-tea.html https://www.example.co.uk/collections/all
Redirect 301 /ginseng-ginger-lime-tea.html https://www.example.co.uk/collections/all
...
http://www.example.com/speciality-tea/
http://www.example.com/product/ginseng-ginger-lime-tea/


if you request the /speciality-tea/ path it won't match the /speciality-tea.html path.
similarly, if you request the /product/ginseng-ginger-lime-tea/ path it won't match the /ginseng-ginger-lime-tea.html path.
etc...


RewriteEngine on

RewriteEngine is a mod_rewrite directive and Redirect is a mod_alias directive.
if you intended to use mod_rewrite directives, you should look at the RewriteRule Directive:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule


also note that redirecting specific product or category pages to a general category page is typically not a good experience.
if those product specific pages are in fact gone, you should respond with a 410 Gone status code and perhaps include a link to the category page from a custom 410 error page.
2:29 am on Aug 18, 2017 (gmt 0)

New User

joined:Aug 17, 2017
posts: 10
votes: 0


Hello Phranque,

Thank you so much for your warm welcome and for your advice. I'm a little bit technical but to be honest, I'm still learning my way around the online world and back-end of websites, so please bear with me as I continue to pick up the pace and learn..

I've actually tried with and without .html and neither of them are correctly redirecting. What would you advise in this scenario? I am certain that I need Redirect 301 in order to redirect the individual pages from the old site to the new site.

Which command should I have in place for the directives to match? If RewriteEngine isn't the correct option, I appreciate a point in the right direction if you wouldn't mind?

Thank you for your advice regarding the redirection of the product specific pages. I'll definitely consider this!
6:32 am on Aug 18, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member tangor is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Nov 29, 2005
posts:7784
votes: 532


Have you verified that REWRITE is activated?
6:43 am on Aug 18, 2017 (gmt 0)

New User

joined:Aug 17, 2017
posts: 10
votes: 0


Hi Tangor,

How would I go about doing this. Do I actually need the mod_rewrite if I just wanted to create permanent 301 page redirects for individual pages?

Thanks.
9:37 am on Aug 18, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10989
votes: 84


I've actually tried with and without .html and neither of them are correctly redirecting.

show us the exemplified version of what you tried - without the .html, that is.

I am certain that I need Redirect 301 in order to redirect the individual pages from the old site to the new site.

you don't necessarily "need Redirect 301" but it is an option.
it is also possible to do this using mod_rewrite's RewriteRule directive.
if you aren't using other mod_rewrite directives the use of mod_alias' Redirect 301 is suitable for one-to-one redirects.

If RewriteEngine isn't the correct option, I appreciate a point in the right direction if you wouldn't mind?

if you aren't using any other mod_rewrite directives you can delete the RewriteEngine on.

Have you verified that REWRITE is activated?

this (quoted from the OP) implies mod_alias is activated:
only the homepage redirect is currently working.
9:44 am on Aug 18, 2017 (gmt 0)

New User

joined:Aug 17, 2017
posts: 10
votes: 0


Hi Phranque,

Thanks once again for your response.

Here are the new examples of what I've tried:

Redirect 301 / https://www.example.co.uk/
Redirect 301 /speciality-tea https://www.example.co.uk/collections/all
Redirect 301 /ginseng-ginger-lime-tea https://www.example.co.uk/collections/all
Redirect 301 /earl-grey-tea https://www.example.co.uk/collections/all
Redirect 301 /tea-ceremony https://www.example.co.uk/blogs/news

The last three links are just leading to an error 404 page, but on the NEW domain. The "speciality-tea" link is simply saying "page not found" so it's not even an error 404. I understand this is because for some reason it's removing a slash from the URL but I have no idea why it's treating this link differently even though I've done exactly the same for the speciality-tea link as I have done for the other three links below it.

With regards to your second point, I have ommitted some code from the .htaccess file but I don't think it impacts on this in any way. It was there preexisting before the site migration and permanent redirects. I believe it was set up to redirect links internally. This is it:

# 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

Will this impact in any way? Here's the full snippet for everything in the .htaccess file:

Redirect 301 / https://www.example.co.uk/
Redirect 301 /speciality-tea https://www.example.co.uk/collections/all
Redirect 301 /ginseng-ginger-lime-tea https://www.example.co.uk/collections/all
Redirect 301 /earl-grey-tea https://www.example.co.uk/collections/all
Redirect 301 /tea-ceremony https://www.example.co.uk/blogs/news


# 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

Thanks in advance.

[edited by: phranque at 10:22 am (utc) on Aug 18, 2017]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]

10:48 am on Aug 18, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10989
votes: 84


However, only the homepage redirect is currently working. These are the URLs I'm trying to redirect to the above URLS that are not working:

http://www.example.com/speciality-tea/
...
http://www.example.com/tea-ceremony/


note the trailing slashes in the old urls...

Redirect 301 /speciality-tea https://www.example.co.uk/collections/all
...
Redirect 301 /tea-ceremony https://www.example.co.uk/blogs/news

assuming http://www.example.com/speciality-tea/ was requested, the Redirect directive will match the url path up to but not including the trailing slash, which will be appended to the target url path, so it should redirect to https://www.example.co.uk/collections/all/ - is that correct?
assuming http://www.example.com/tea-ceremony/ was requested, the Redirect directive will match the url path up to but not including the trailing slash, which will be appended to the target url path, so it should redirect to https://www.example.co.uk/blogs/news/ - is that correct?

see this...
https://httpd.apache.org/docs/current/mod/mod_alias.html#redirect
any request beginning with URL-path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-path will be appended to the target URL.


http://www.example.com/product/ginseng-ginger-lime-tea/
http://www.example.com/product/earl-grey-tea/


note the /product/ in the url paths...

Redirect 301 /ginseng-ginger-lime-tea https://www.example.co.uk/collections/all
Redirect 301 /earl-grey-tea https://www.example.co.uk/collections/all


neither of these requested url paths will match the paths you specified in the Redirect directives.

With regards to your second point, I have ommitted some code from the .htaccess file but I don't think it impacts on this in any way. It was there preexisting before the site migration and permanent redirects. I believe it was set up to redirect links internally. This is it:

# BEGIN WordPress

if it's a wordpress site you want to be using mod_rewrite and RewriteRule instead of mod_alias directives.

from the apache documentation...
https://httpd.apache.org/docs/current/rewrite/avoid.html#redirect
The use of RewriteRule to perform this task may be appropriate if there are other RewriteRule directives in the same scope. This is because, when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file.


RewriteRule Directive:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
11:07 am on Aug 18, 2017 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:3451
votes: 181


Using Mod_Alias (the Redirect 301 part) when also using Mod_Rewrite (the WP cond/rule set) can always cause unexpected results. The homepage will always resolve to the URL you entered into the WordPress settings. My suggestion would be to use Mod_Rewrite for all of your 301's since you need to use it for the WP install. Are the .html pages on the new domain or only on the old domain?
5:12 pm on Aug 18, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14045
votes: 523


Even before you mentioned WordPress, the chances were approximately zero that your site does not use mod_rewrite (RewriteRule) at all. If nothing else, there will always be a domain-name-canonicalization redirect. Now that you have mentioned WordPress, it becomes “exactly zero”. So you need to get rid of all mod_alias directives (Redirect by that name) and change them to mod_rewrite syntax. If there are lots of them, there are some global replaces you can do. But it sounds more as if you're creating new redirects, so all you have to do is get them in the right form to start with.

Since WordPress is involved, it is important to put your redirects in the right place, which is to say before the WordPress envelope. (The <IfModule blahblah business is stupid and unnecessary, since WP relies on mod_rewrite to function in the first place--but it's WP's stupidity, not yours, so it has to stay. Nothing to be done.)

At the beginning, you said there were only a few specific URLs that needed redirecting. Ordinarily it's not a good idea to redirect lots of URLs to the same place--but if it's only two or three of them, we're still OK. Note however that you can redirect to a fragment. So for example if there is a particular place on
www.example.co.uk/collections/all
that talks specifically about Earl Grey tea, you can redirect to that place.

As originally posted:
Redirect 301 / https://www.example.co.uk/
Redirect 301 /speciality-tea.html https://www.example.co.uk/collections/all
Redirect 301 /ginseng-ginger-lime-tea.html https://www.example.co.uk/collections/all
Redirect 301 /earl-grey-tea.html https://www.example.co.uk/collections/all
Redirect 301 /tea-ceremony.html https://www.example.co.uk/blogs/news

Translating this into mod_rewrite syntax, we have just three rules:

RewriteRule ^(index\.html)?$ https://www.example.co.uk/ [R=301,L]

RewriteRule ^(speciality|ginseng-ginger-lime|earl-grey)-tea\.html https://www.example.co.uk/collections/all [R=301,L]

RewriteRule ^tea-ceremony\.html https://www.example.co.uk/blogs/news [R=301,L]

In fact you may not even need the shared "-tea\.html" element, assuming you don't have, for example, a not-to-be-redirected /specialty-coffees.html or whatnot. As long as you don't use a closing anchor, it makes no difference.

Leave a blank line after each RewriteRule. The server doesn't care; it's for your own ease in reading. But see above about redirecting to fragments, if appropriate.

See the (index\.html)? part? That's for search engines; they like to try things with and without "index.html" to see if both versions resolve.

Incidentally, the fragment redirect in its easiest form might look like this:
RewriteRule ^(speciality|ginseng-ginger-lime|earl-grey)-tea\.html https://www.example.co.uk/collections/all#$1 [R=301,L,NE]
But only if that's really what you are doing.
7:23 pm on Aug 18, 2017 (gmt 0)

New User

joined:Aug 17, 2017
posts: 10
votes: 0


Hello all,

Thank you for your responses. As I mentioned previously. I have a moderate technical understand and am somehow keeping up but just need a bit of time to make sense of it. I think your response Lucy24 has been really helpful.

I just couldn't find anything in my search that gave a clear example and explanation on how to use the Mod_Rewrite specifically for my needs.

I'll test out what you just sent me and understand how you reached the code you reached.

Thanks so much for all of your responses I really appreciate the amazing responses so far. I'm really glad I found this forum.
3:55 pm on Aug 21, 2017 (gmt 0)

New User

joined:Aug 17, 2017
posts: 10
votes: 0


Hello Lucy,

Further to your suggestion. I attempted the code you supplied and it's taking to a Page Not Found error. Also, I tried possible removing the .html from the original URLs in case this was causing an issue, but it's still "Page Not Found"

I also tried removing the WordPress code snippet as I felt there may be a clash between that code and these redirects. Any guidance would be appreciated and thanks for all your help so far.
4:33 pm on Aug 21, 2017 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:3451
votes: 181


If you removed the WP snippet, you will quite definitely have problems with your WP URLs. The Settings depend on that snippet to tell it how to incorporate your Settings, it delivers the home page for various taxonomy URLs such as /category/, /archives/, /tags/ and since these files don't exist in those directories that don't exist, you really do need that snippet for WP to operate as expected.

When you see the Page Not Found error, what URL is in your browser's address bar? That will tell you where it needs work.

The WP snippet needs to be placed after all other rewrites/redirects.
4:45 pm on Aug 21, 2017 (gmt 0)

New User

joined:Aug 17, 2017
posts: 10
votes: 0


Hi Not2easy,

Thanks for the heads up. I'll add it back no problem. In the page not found error this is what I get:

The requested URL /tea-ceremony/ was not found on this server.

This is the snippet I have in place to redirect that URL:

RewriteRule "^tea-ceremony\.html$" "https://www.example.co.uk/blogs/news/" [R=301,L]

If I can make it work for one of them I can make it work for all of them. I made some small adjustments to the code Lucy provided based on some research, but to no avail.

Edit: I might add it's just weird that the redirect is working fine for the homepage, but not any of the sub pages. no matter what I try!


[edited by: not2easy at 5:41 pm (utc) on Aug 21, 2017]
[edit reason] Please use 'example.co' in place of your domain name [/edit]

6:05 pm on Aug 21, 2017 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:3451
votes: 181


I'm sorry I needed to edit your domain name again, please use "example" in place of the domain name, it prevents auto-linking and complies with the WebmasterWorld Terms of Service [webmasterworld.com]

Have you created categories called /blogs/ and /news/ and created the page related to that category and subcategory where people can find the old html content (or related updated content)? You would need to have all that in place with /news/ being an actual category that is assigned to the category of /blogs/ (which would also need to be an actual category). I don't see any way the rule you have posted could possibly lead to "/tea-ceremony/". There should not be any quotes-"- around the rule or the target though.

If you tried the rule that lucy24 suggested and it "did not work" what did it do? If the destination URL does not exist, it won't be created, it needs to exist.

The homepage is redirected by the WP snippet (unless we are talking about a WP install in a subdirectory of /blogs/ with a directory of /news/).
6:22 pm on Aug 21, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10989
votes: 84


when you report your findings you should include the requested path and (the complete redirect chain to if any) the url which caused the error.
you should also include which rulesets you use to test each request.
as it reads now you are essentially reporting "i tried a bunch of things and some of it didn't work".

The requested URL /tea-ceremony/ was not found on this server.

This is the snippet I have in place to redirect that URL:

RewriteRule "^tea-ceremony\.html$" "https://www.example.co.uk/blogs/news/" [R=301,L]


as i mentioned in my previous post, this pattern won't match the requested path.
you need to match a trailing slash, not a .html suffix.

also wondering, why are you using double quotes in the RewriteRule?
6:25 pm on Aug 21, 2017 (gmt 0)

New User

joined:Aug 17, 2017
posts: 10
votes: 0


Oh apologies for that I didn't realise that was the case. I'll ensure to do that in future.

All of the pages that are being redirected to do indeed exist. The new site is on Shopify though so not sure if the fact the new site is on a different CMS changes anything. After tryijng the rule that Lucy suggested, it simply just takes me to the Page Not Found error that I mentioned earlier. I'm 100% sure that I implemented it in exactly the way that was demonstrated to me in Lucy's post, but the only redirect that works is the one for the homepage. Even after I temporarily removed the WP snippet, the homepage redirect was still working based on this part of the rule: RewriteRule ^(index\.html)?$ https://www.example.co.uk/ [R=301,L]

I'm sure that part is working because I tested it without the WP snippet and it was still working fine.
6:39 pm on Aug 21, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10989
votes: 84


The new site is on Shopify

is any of the site using WP?
where is the internal rewrite to the Shopify script?
6:57 pm on Aug 21, 2017 (gmt 0)

New User

joined:Aug 17, 2017
posts: 10
votes: 0


Hi Phranque,

I tried with a trailing slash, without a trailing slash, with and without a .html suffix but getting the same result. I chose to use double quotes after looking at the syntax online for Rewrite rules and this is how it was suggested to do.

Could you explain what you mean by describe the ruleset I was using? I'm assuming you mean which code I actually used? Or something else maybe?

Thanks.
7:03 pm on Aug 21, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14045
votes: 523


If you want to redirect requests for
/tea-ceremony.html
then write the rule for
^tea-ceremony\.html
Closing anchor is not needed, but will do no harm.

If, instead, you want to redirect requests for
/tea-ceremony/
then write the rule for
^tea-ceremony/$
Closing anchor is only needed if the old URL is in a directory (real or pseudo) containing other, not-to-be-redirected URLs; otherwise you can say
^tea-ceremony
omitting both / and anchor, as this will also pick up search-engine requests for
^tea-ceremony$
and
^tea-ceremony/index.html
(Search engines really do this when an URL ends in / slash.)

Do the two different domains--call them example.old and example.new--live on different servers?
7:06 pm on Aug 21, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10989
votes: 84


I tried with a trailing slash, without a trailing slash, with and without a .html suffix but getting the same result.

please post what you tried with the trailing slash since we haven't seen that and the other cases are irrelevant.
post the relevant mod_rewrite rulesets, the url requested (exemplified), and the status code of the response.
if any responses are 3XX status code (301, 302,...) then also report the value of the Location header for the complete redirect chain.
11:37 pm on Aug 21, 2017 (gmt 0)

New User

joined:Aug 17, 2017
posts: 10
votes: 0


Hey all,

Happy to say it finally worked! Thank you very much Lucy for your last post. The way that it worked was ^tea-ceremony/$. I think the $ on the end made the difference.

Thanks to all who contributed as well. I was finding it a little difficult to follow in some parts just because I didn't understand some of the terminology. I'm very new to Apache server code so it was a bit of a jump in the deep end for me. I'll definitely be scouring these forums more to learn more!

Thank you all for your help and patience.
2:41 am on Aug 22, 2017 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8510
votes: 228


The new site is on Shopify though so not sure if the fact the new site is on a different CMS changes anything.


Mostly it just means that only the first rule evaluated will come into effect as, of course, after the first redirect you are on a new server and the .htaccess file will never be seen again. If your redirects are set up so that none of them require two hops, you should be set.


is any of the site using WP?
where is the internal rewrite to the Shopify script?


The .com is using Wordpress.
The .co.uk is on Shopify. Shopify is not a script, it is a hosted e-commerce platform. Totally separate server with completely different technology.

RewriteRule ^(speciality|ginseng-ginger-lime|earl-grey)-tea\.html https://www.example.co.uk/collections/all [R=301,L]


I don't think that's what you want. Presumably the teas are a collection, and the ones listed are individual products. I assume you need your current /earl-grey-tea.html to redirect to /product/earl-grey-tea on Shopify.

This is slightly more verbose than needed, but I'm hoping it will make it easier for you to see what's happening
RewriteRule ^(speciality-tea|ginseng-ginger-lime-tea|earl-grey-tea)\.html https://www.example.co.uk/product/$1 [R=301,L]


You could make it a bit shorter with

RewriteRule ^(speciality|ginseng-ginger-lime|earl-grey)-tea\.html https://www.example.co.uk/product/$1-tea [R=301,L]



That will grab the first part of the URL minus the .html, and add it to the /product/ and that should match your new page on Shopify. If your page URLs don't match, just change them in Shopify and make your life easy (bottom of the product page, under SEO, you can set the URL to more or less whatever you want).