Opencar 301 Redirecting issue with ?route= - .htaccess

I have restructure the website from WordPress to Opencart and so all URL has been changed. I am trying to redirect my old products page with new but it is not redirecting since OpenCart adding some weird ?route= and so on..
Here is my .htaccess
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
Redirect 301 /store/products/old-item-name/ http://store.mydomain.com/new-item-name
It is redirecting to this weird url
http://store.mydomain.com/new-item-name?_route_=store/products/old-item-name/
I appreciate your great help.. million thanks

This is because the Redirect is part of mod_alias and everything else is part of mod_rewrite. The two modules get applied at different points of the URL-file processing pipeline, and in this case your request is getting applied by both modules. You need to move the redirect up to the top and stick to just mod_rewrite:
So remove:
Redirect 301 /store/products/old-item-name/ http://store.mydomain.com/new-item-name
and add this to the top:
RewriteRule ^products/old-item-name(/.*)$ http://store.mydomain.com/new-item-name$1 [L,R=301]

Last Redirect 301 rules is getting redirected by previous RewriteRule rule. In general its not advisable to mix mod_alias and mod_rewrite:
Try changing the last RewriteRule to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# your 301 rules from old to new
RewriteRule ^products/old-item-name(/.*|)$ new-item-name$1 [NC,L,R=301]
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase\.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(ico|gif|jpe?g|png|js|css)$ [NC]
RewriteRule ^(.*)$ index.php?_route_=$1 [L,QSA]

have just had a similiar problem when migrating from Ashop to OpenCart
the old urls were of the format
http://www.example.com/p/productid/manufacturer-productname.html
and were being redirected to
http://www.example.com/manufacturer/
the solutions was
RewriteRule ^(.*)manufacturer(-.*|)$ http://www.example.com/manufacturer/ [NC,L,R=301]
hope that helps someone.....
note you cannot directly match on manufacturer as a redirect loop will happen so match included the '-'

Related

301 redirect of pages not working opencart

direct some pages to the homepage of opencart.
For examle:
https://mywebsitenamehere.com/index.php?route=product/manufacturer
301 redirect to:
https://mywebsitenamehere.com/
I would usually add something like this:
Redirect 301 /index.php?route=product/manufacturer https://mywebsitenamehere.com/
Snippet of my .htaccess:
# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/storage/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
Thank you!!
Could you please try following, written and tested with shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/storage/(.*) index.php?route=error/not_found [L]
RewriteCond %{QUERY_STRING} ^route=product/manufacturer [NC]
RewriteRule ^index\.php https://mywebsitenamehere.com [R=301,NC,L]
UPDATE:
in (for example)
manufacturer.php
add the following to the very top:
<?php // to redirect page
header( 'Location: https://mywebsitenamehere.com/' ) ;
?>

How can I redirect a single segment url without forward slash at the end?

My website's htaccess file is not allowing a single segment's redirect without a forward slash at the end. If I leave it off, it loops and crashes the website. How can I allow my users to go to [site].com/winter-camping and have it be redirected? This is what I have set up that currently works:
RewriteEngine On
RedirectMatch permanent /find-a-park/ https://[site].com/about/find-a-park
RedirectMatch permanent /winter-camping/ https://[site].com/about/promotion-details/winter-camping
#our rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Basically I don't want my users to have to put the forward slash at the end to get to the page at all.
I do have instances that do work, however, which is why I'm confused. The only difference is they're subpages:
RedirectMatch permanent /parks/about/central-park https://[site].com/parks/central-park
One thing to know is that we use ExpressionEngine as our CMS. How can I get those first two redirects to redirect their urls and not crash my site? Thanks 🙃
You should use only mod_rewrite rule and use a regex with optional trailing slash:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^find-a-park/?$ https://[site].com/about/find-a-park [L,NC,R=301]
RewriteRule ^winter-camping/?$ https://[site].com/about/promotion-details/winter-camping [L,NC,R=301]
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

OpenCart - htaccess don't work - no redirect 301

I have this htaccess, but the 301 redirect don't work.
Options +FollowSymlinks
# SEO URL Settings
RewriteEngine On
RewriteBase /shop/
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^system/download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
I want to redirect a basic url, but it doesn't work, I get a "404 page not found"-Message.
I tested the following, but nothing worked.
Redirect 301 ^test123$ http://www.domain.com/shop/notebook-reparatur/acer
Redirect 301 /test123 http://www.domain.com/shop/notebook-reparatur/acer
RewriteRule ^test123.*$ http://www.domain.com/shop/notebook-reparatur/acer/ [R]
Can anyone help me in my basic problem, I don't undestand why it doesn't work..
thanks, regards
recep779

Rewrite rule htaccess disturbing other rewrite rules

I have a page on my website that is getting generated dynamically to list all outlets based on cityf parameter and below is rewrite rule to convert it into SEO friendly URL and it is working pretty well.
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]
I have a blog page on my website and .htaccess is as below to convert SEO Friendly URL (http://example.com/title-of-blog)
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
Now the problem here i am facing that when someone visits blog page then the link http://example.com/title-of-blog instead of displaying blog detail on the page, displays my Error message that No outlets near title-of-blog.
I got the issue that Apache is not able to identify when to rewrite cityres page and when to rewrite blogdetail page.
Someone suggested that Make sure that each rule has a common prefix (e.g. /blog/page1 and /news/page2). but i did not get that.
Any suggestions here please?
EDIT:
Whole htaccess is as below
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index
RewriteRule ^index\.php$ / [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# remove .php from URL
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)\.html$ /$1 [L,R=301]
ErrorDocument 404 /error-page
ErrorDocument 403 /error-page
RewriteRule ^food-([^-]*)-([^-]*)\.html$ /pdetail?res_id=$1&location=$2 [L]
RewriteRule ^foodies-([^-]*)-([^-]*)$ /pdetail_new?res_id=$1&location=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /pdetail_ne?location=$1&res_id=$2&name=$3 [L]
RewriteRule ^blog/([^/.]+)/?$ /blogdetail_fm?prmn=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !cityres
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]
Both your rules match the exact same pattern. Therefore, the first rule will always match and the second rule does nothing.
Looking at the first rule:
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
This matches http://example.com/title-of-blog as well as http://example.com/city-name
When you look at it, you can tell which needs to be handled by blogdetail and which needs to be handled by cityres, but the regex ([^/.]+) sees them both as exactly the same, and matches both. Your regex doesn't know the difference, so whatever the first rule is, both URL's will get matched by it.
Like you said, someone suggested using a prefix. That way, the regex knows which is which:
RewriteRule ^city/([^/.]+)/?$ /cityres?cityf=$1 [L]
RewriteRule ^blog/([^/.]+)/?$ /blogdetail?prmn=$1 [L]
ANd your URLs will look like:
http://example.com/city/city-name
http://example.com/blog/title-of-blog
If you're really hung up about not adding prefixes, you can remove the second prefix:
RewriteRule ^city/([^/.]+)/?$ /cityres?cityf=$1 [L]
RewriteRule ^([^/.]+)/?$ /blogdetail?prmn=$1 [L]
So that you have:
http://example.com/city/city-name
http://example.com/title-of-blog
EDIT:
Your 500 server error is caused by the rules looping. You need to add a condition so that they won't keep matching:
RewriteRule ^blog/([^/.]+)/?$ /blogdetail?prmn=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !cityres
RewriteRule ^([^/.]+)/?$ /cityres?cityf=$1 [L]

Help with mod_rewrite and mod_redirect

My .htaccess file is:
Redirect 301 http://domain.com/news/articles?dtMain_start=150 http://domain.com/news/articles
Redirect 301 http://domain.com/news/articles?dtMain_start=160 http://domain.com/news/articles
Redirect 301 http://domain.com/news/articles?dtMain_start=170 http://domain.com/news/articles
#
RewriteEngine On
RewriteBase /
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I also have to incorporate the following rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.co.uk/$1 [R=301,L]
I cannot get them to work together... can anyone help...
I tried just stacking the Redirects before the RewriteCond and I get this...
http://www.domain.com/news/articles?q=news/articles?dbMain_start=150
ie http://domain.com/newpage?q=oldpage
Okay Mod_Alias and Mod_Rewrite don't like each other.
Can I write something like:
RewriteCond %{REQUEST_QUERY_STRING} ^.*&bodgeredirect=true$
RewriteRule ^(.*)&bodgeredirect=true$ index.php?q=$1 [L,QSA]
First of all: There is not mod_redirect. Redirect is a directive of mod_alias.
And the Redirect directive, like any other directive of mod_alias, does only work with the URL path. So your Redirect directives won’t work as expected. Use mod_rewrite equivalents instead:
RewriteCond %{HTTP_HOST} =example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^dtMain_start=(150|160|170)$
RewriteRule ^news/articles$ /news/articles? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
In general it is not a good idea to mix mod_alias and mod_rewrite if the patterns coincide with each other.

Resources