htaccess RewriteRule and Redirect 301 don't work - apache

We're trying to set up 301 redirects to make sure Google uses the right category pages. We can't set up a canonical and a normal Redirect 301 /n.html https://website.com/n.html doesn't work either, it's just ignored. We're on Opencart 1.5.5.1. The issue is that the 301s are just ignored, they're not implemented at all both Redirect 301 and RewriteRule
# SEO URL Settings
RewriteEngine On
RewriteRule ^/refrigeration/Multidecks/fresh-meat-multidecks$ https://www.website.co.uk/Multidecks/fresh-meat-multidecks [R=301,L]
RewriteRule ^/fresh-meat-multidecks$ https://www.website.co.uk/Multidecks/fresh-meat-multidecks/ [R=301,L]

Like Anubhava said, the issue is the / after the ^

Related

.htaccess - redirect, if a specific url match

I need to redirect users to a new domain, but I need that in case a user visits a particular URL, then he/she needs to be redirected to another URL.
Let me clarify...
If the user visits http://oldexample.com/postvendita/ I need to redirect them to http://newexample.com/assistenza
Otherwise, for every other URL http://oldexample.com/* I need to redirect them to http://newexample.com/new-page
Here is my attempt:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)postvendita(.*)$ http://www.newexample.com/assistenza [L,R=301]
RewriteCond %{HTTP_HOST} !^oldexample\.com$ [NC]
RewriteRule ^(.*)$ http://www.newexample.com/new-page [R=301,L]
Now if I visit any of the old pages, I will be redirected to http://www.newexample.com/new-page, so the first rule doesn't work, how should I change it?
To handle this via .htaccess you'll want to match the first one and use a catch-all to redirect everything else:
RewriteEngine On
RewriteRule ^postvendita/?$ http://www.newexample.com/assistenza [R=301,L,NE]
RewriteRule .* http://www.newexample.com/new-page [R=301,L,NE]
A 301 redirect is a permanent redirect which passes between 90-99% of
link juice (ranking power) to the redirected page. 301 refers to the
HTTP status code for this type of redirect. In most instances, the 301
redirect is the best method for implementing redirects on a website.
More About Redirects
Alternatively, if you're not comfortable writing RewriteRules, you can use the following lines:
Redirect 301 /postvendita/ http://www.newexample.com/assistenza
Redirect 301 /postvendita http://www.newexample.com/assistenza
Redirect 301 / http://www.newexample.com/new-page
Or with RedirectMatch:
RedirectMatch 301 /postvendita/? http://www.newexample.com/assistenza
RedirectMatch 301 .* http://www.newexample.com/new-page
Common .htaccess Redirects

redirect certain pages to its equivalent in another domain, otherwise redirect to its homepage

I want to use htaccess to redirect certain pages to its equivalent in another domain, other pages redirect to homepage.
Like that:
http://old-domain.com/certain-page redirected to: http://new-domain.com/certain-page
http://old-domain.com/another-certain-page redirected to: http://new-domain.com/another-certain-page
Only these pages will be redirected, otherwise, pages have to be redirected to the new domain home page.
http://old-domain.com/non-certain-page redirected to: http://new-domain.com
This is my try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^certain-page$ http://new-domain.com/certain-page [R=301,L]
RewriteRule ^another-certain-page$ http://new-domain.com/another-certain-page [R=301,L]
</IfModule>
But don't know how to exclude other pages.
Any help here?
Thanks in advance!
I'm not sure what you mean by
But don't know how to exclude other pages
But you are already excluding the pages when you create specific rules for them. This should work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#redirect specific pages
RewriteRule ^certain-page$ http://new-domain.com/certain-page [R=301,L]
RewriteRule ^another-certain-page$ http://new-domain.com/another-certain-page [R=301,L]
#Redirect everything else to homepage
RewriteRule ^(.*)$ http://new-domain.com/ [R=301,L]
</IfModule>
Let me know how this works for you. If you need to clear you cache before trying these new rules.
Another answer was mentioned here, thank to anubhava
With redirect without need to use Rewrite :
RedirectMatch 301 ^/.+ http://www.newdomain.com/
So, the answer can be also:
#Redirect specific pages
redirect 301 /certain-page http://new-domain.com/certain-page
redirect 301 /another-certain-page http://new-domain.com/another-certain-page
#Redirect everything else to homepage
RedirectMatch 301 ^/.+ http://new-domain.com

htaccess redirect page on Magento multi store

We need to set up a htaccess redirect from one page to a directory, so this works:
Redirect 301 /page-name.html http://www.domain-01.co.uk/directory
But we have multiple domains set up through Magento so need to do this:
http://www.domain-01.co.uk/page-name.html redirect to http://www.domain-01.co.uk/directory
http://www.domain-02.co.uk/page-name.html redirect to http://www.domain-02.co.uk/directory
And these don't work:
Redirect 301 http://www.domain-01.co.uk/page-name.html http://www.domain-01.co.uk/directory
Redirect 301 http://www.domain-02.co.uk/page-name.html http://www.domain-02.co.uk/directory
or:
RewriteCond %{HTTP_HOST} ^www.domain-01.co.uk/page-name.html
Rewriterule ^(.*)$ http://www.domain-01.co.uk/directory [R=301,L]
RewriteCond %{HTTP_HOST} ^www.domain-02.co.uk/page-name.html
Rewriterule ^(.*)$ http://www.domain-02.co.uk/directory [R=301,L]
I've looked the documentation (http://httpd.apache.org/docs/2.0/misc/rewriteguide.html#ToC1), but I can't see anything that relates to this.
The problem seems to me to be how do include a domain name in the first part of a redirect? Or am I asking the wrong question?
If you want it for every domains, you only have to do it like this
RewriteRule ^page-name\.html$ /directory [R=301,L]

How to combine Apache redirects?

I've got an Apache config that features multiple rewrite rules and redirects in order to get the cutest URLs, prevent duplicates for SEO, etc. Here's a snippet as an example (it features a lot more):
# Redirecting non-www to www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# Removing index.php
RewriteCond %{REQUEST_URI} index\.php$ [NC]
RewriteRule .* / [R=301,L]
# A big bunch of these
Redirect permanent /old-article.html http://www.example.com/new-article.html
Redirect permanent /another-old-article.html http://www.example.com/new-article2.html
This works well, but it happens to generate a lot of redirects. A common case looks like this:
http://example.com/index.php, 301 redirect to http://www.example.com/index.php
http://www.example.com/index.php, 301 redirect to http://www.example.com
It sometimes reaches 4-5 redirects.
Now, I want all these rules to be chained and generate only one 301 redirect, like this:
http://example.com/index.php, 301 redirect to http://www.example.com
I know I can spend an afternoon thinking and sorting the rules to a better match, and also that I can create combined rules. But that would complicate an already long file. I want a flag, operand or whatever that will execute all the rules as if they where internal and only issue the redirect once it has crawled every rule. Is this even possible?
It seems as if simply re-ordering this would get you what you want:
# A big bunch of these
Redirect permanent /old-article.html http://www.example.com/new-article.html
Redirect permanent /another-old-article.html http://www.example.com/new-article2.html
# Removing index.php
RewriteRule ^/index.php http://www.example.com/ [R=301,L]
# Redirecting non-www to www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
A direct request for an old article at the example.com domain:
http://example.com/old-article.html
Will result in a single redirect to:
http://www.example.com/new-article.html
A request for either http://example.com/index.php or http://www.example.com/index.php will result in a single redirect to:
http://www.example.com/
A request that doesn't match anything else will result in a single redirect from:
http://example.com/foo
To:
http://www.example.com/foo
This seems to cover all the bases. Have I missed anything?
Remove the [L] flag from your RewriteRules, and they will be combined automatically.

Htaccess 301 only part of the redirect works

Hi Im moving a site from one domain to another, and I have created the following .htaccess file, but its not working.
*#Options +FollowSymlinks
RewriteEngine On
redirect 301 http://www.el-netshop.dk/pi/Dækkape_UG150_12_lysegrå_5302_.aspx http://www.el-netsalg.dk/pi/Dækkape_UG150_12_lysegrå_5271_.aspx
RewriteCond %{HTTP_HOST} ^el-netshop.dk$ [OR]
RewriteCond %{HTTP_HOST} ^www.el-netshop.dk$
RewriteRule (.)$ http://www.el-netsalg.dk/$1 [R=301,L]
I would like it to work like this.
Have a list of urls where the url is diffent, with more then just the domain. Ex. in the above the from link contains 5302 but to link is 5271.
Then with the rest, I want it to make a normal redirect.
The above code just do (.*)$ http://www.el-netsalg.dk/$1 and ignores the special cases.
What am I doing wrong?
According to the apache docu the syntax is as folows:
Redirect 301 /service http://foo2.bar.com/service
So try:
Redirect 301 /pi/Dækkape_UG150_12_lysegrå_5302_.aspx http://www.el-netsalg.dk/pi/Dækkape_UG150_12_lysegrå_5271_.aspx
without the "http://www.el-netshop.dk" for the old-path paramater.