Removing Categories from the product url

Currently we have the SEO addon set to “category name/sub category/productname.html”. We want to change this to just “product_name.html”. We know that we must setup a 301 redirect in order to redirect traffic to the new urls. Does anyone know what to correct rewrite rule would be?

Hello idslamyou,



Thank you for your message.



You can use the following redirect rule in the .htaccess file located in the root directory of your CS-Cart installation:


RewriteCond %{REQUEST_URI} .*\/category_name\/sub_category\/product_name.html
RewriteRule ^.*$ http://yourdomain.com/product_name.html [L,R=301]




Place it right after this line:


RewriteBase /



Please check it.



Thank you.





Pavel Zyukin

CS-Cart Support team

That did not work I still get a 404 page not found when the old url is accessed. I even tried adding the another condition to the rules after the first would not work.


RewriteCond %{REQUEST_URI} .*\/category_name\/sub_category\/product_name.html
RewriteRule ^.*$ http://mydomain.com/product_name.html [L,R=301]
RewriteCond %{REQUEST_URI} .*\/category_name\/product_name.html
RewriteRule ^.*$ http://mydomain.com/product_name.html [L,R=301]


RedirectMatch 301 ^/old-category/(.*)$ http://www.yourdomain.com/$1




This will turn yourdomain.com in to yourdomain.com

[quote name='StellarBytes' timestamp='1356727138' post='151859']


RedirectMatch 301 ^/old-category/(.*)$ http://www.yourdomain.com/$1




This will turn yourdomain.com in to yourdomain.com

[/quote]



Quick question, do I have to change old-category to the actual category name? Also, will this work for a url that is mydomain/category/subcategory/productname.html as well?

Change 'old-category' to the name of your category, yes. So if your product URL's are like this:



yourdomain.com

yourdomain.com



Use these rules:


RedirectMatch 301 ^/your-category-1/(.*)$ http://www.yourdomain.com/$1
RedirectMatch 301 ^/your-category-6/(.*)$ http://www.yourdomain.com/$1




To permanently redirect (ie. 301 redirect) to:



yourdomain.com

yourdomain.com



With regards to the subcategory, you'll need a separate rule for each subcategory, like this:


RedirectMatch 301 ^/your-category-1/sub-category-1(.*)$ http://www.yourdomain.com/$1
RedirectMatch 301 ^/your-category-1/sub-category-2(.*)$ http://www.yourdomain.com/$1




You will want to have the parent category rule last, as .htaccess is processed top-down, like so:


RedirectMatch 301 ^/your-category-1/sub-category-1(.*)$ http://www.yourdomain.com/$1
RedirectMatch 301 ^/your-category-1/sub-category-2(.*)$ http://www.yourdomain.com/$1
RedirectMatch 301 ^/your-category-1/(.*)$ http://www.yourdomain.com/$1

Hey StellarBytes (or anyone who can chime in),



Your rewrite rules work perfectly for the individual product pages, however, it also redirects the category page to the homepage.



What do you think I can add/remove to make sure the category page is not being redirected and only the products within the category are?



Thanks for any help

Figured it out friends!



Using the following rule format shared by StellarBytes:


RedirectMatch 301 ^/your-category-1/(.*)$ http://www.yourdomain.com/$1



Simply change the [color=#0000ff]*[/color] to a [color=#ff0000]+[/color], like this:


RedirectMatch 301 ^/your-category-1/(.+)$ http://www.yourdomain.com/$1







Hope this helps others!

Actualllyyyy…



This doesn't work properly with sub-categories. The individual products within the sub-categories do redirect properly to their new URL's, but the sub-category page redirects incorrectly.



When you try to go to the sub-category like this,

domain.com/top-category/sub-category/

It redirects to,

domain.com/sub-category/

Resulting in 404 error



Anyone have a thought?



Thanks

Guess I solved this one for myself on my own.



Assuming you have read through the conversation above:



If you have products within sub-categories, then only reference the subcategory, do not reference the top-level category.



So, if you have products within domain.com/category3/sub-category9,

then do not create a redirect for /category3/, only create redirects for all the subcategories within, such as,

/category3/sub-category7

/category3/sub-category8



BAM!

Hi,



you have to use something like this:


```php RewriteEngine on

301 for CS-Cart categories ####

RewriteCond %{query_string} idCategory=1234

RewriteRule (.*) yourdomain.com? [R=301,L]


301 for CS-Cart products ###

RewriteCond %{query_string} idproduct=1234

RewriteRule (.*) yourdomain.com? [R=301,L] ```

Hey Martfox,



Your solution looks like you would need to create a unique redirect for every single product in the store; am I seeing that correctly?



Would your solution help remove the “?sef_rewrite=1” from the end of the redirected URL's?

[quote name='shawnj' timestamp='1365318555' post='159531']Hey Martfox,



Your solution looks like you would need to create a unique redirect for every single product in the store; am I seeing that correctly?[/quote]



Yes, there isn't another way to do that via .htaccess.


[quote]Would your solution help remove the “?sef_rewrite=1” from the end of the redirected URL's?[/quote]



Just try it out, can't say if it will work in your case but it has been used many times ago by our CS-Cart customers and it was working well.

Thanks for your advice, but my solution above gets the job done for me; I’d rather not spend time creating a unique redirect for each product.



If you have any knowledge about removing the pesky “?sef_rewrite=1” from the end of the redirected URL’s, that would be nice! :-)

[quote name=‘shawnj’ timestamp=‘1365360292’ post=‘159556’]

Thanks for your advice, but my solution above gets the job done for me; I’d rather not spend time creating a unique redirect for each product.



If you have any knowledge about removing the pesky “?sef_rewrite=1” from the end of the redirected URL’s, that would be nice! :-)

[/quote]



I think, there isn’t another way as to rewrite the URLs one by one (for products)… But an another question - are all your product URLs indexed?

The majority of my product URL's are indexed by google… maybe we are having a misunderstanding, but the solution I used above works perfectly for redirecting every product to the new URL, I did not have to create a rewrite for each product.



RedirectMatch 301 ^/your-category-1/(.+)$ http://www.yourdomain.com/$1

Hi,



you can try something like this with mod_rewrite instead:


```php

DirectoryIndex index.html index.php

Options +FollowSymLinks





RewriteEngine on


/abc.html to /def.html

RewriteRule ^abc.html$ /def.html [R=301,L]


/abc and /abc/ to /def

RewriteRule ^abc/?$ /def [R=301,L]


/ghi, /ghi/ and /ghi/* to /jkl

RewriteRule ^ghi(/(.*))?$ /jkl/$2 [R=301,L]



RewriteCond %{SCRIPT_FILENAME} !-f

RewriteCond %{SCRIPT_FILENAME} !-d

RewriteRule . ./index.php?sef_rewrite=1 [QSA,L]



```