htaccess - redirect rule on directory - .htaccess

My site currently contains URL's like so:
mysite.com/accessories/bags
mysite.com/clothes/dresses
mysite.com/clothes/dresses/maxi
mysite.com/clothes/dresses/maxi/products
mysite.com/clothes/skirts
mysite.com/clothes/tops
mysite.com/clothes/tops/party
mysite.com/shoes
I now need to redirect any URL's that contain the clothes directory to another domain but keep the same structure.
Is there a rewrite rule where I can do something along the lines of:
mysite.com/clothes/* => newsite.com/clothes/*
i.e
mysite.com/clothes/dresses => newsite.com/clothes/dresses
mysite.com/clothes/dresses/maxi/products => newsite.com/clothes/dresses/maxi/products
And work for however many subfolders within clothes
I had been doing
redirect 301 /clothes/dresses https://www.newsite.com/clothes/dresses
But there are approx 2000 URL's with this instance so one rul to cover all would be good if possible

You can do this by using a simple Redirect directive
Redirect 301 /clothes/ https://www.example.com/clothes/
This will not just redirect /clothes/ but also /clothes/foobar to https://www.example.com/clothes/foobar .

Related

Problem with redirection of shop subcategories in .htaccess

I couldn't find an answer, how to redirect certain subcategories...
I've old shop with paths like that:
shop.com/old_cat1/old_cat2/old_cat3
What I want, is to redirect it like that:
shop.com/new_cat2
shop.com/new_cat3
When I'm trying to use this code:
Redirect 301 /old_cat1/old_cat2/ https://shop.com/new_cat2
Redirect 301 /old_cat1/old_cat2/old_cat3 https://shop.com/new_cat3
redirection from old_cat2 to new_cat2 is working, but redirection to new_cat3 is sending me to new_cat2 (higher level). What should I do? I was trying with RedirectMatch, but it gave me nothing.

What is the correct method to 301 redirect 500+ pages

I need to redirect 500+ pages. The original url references the product id, now they want it to reference the product name. For example:
from:
www.thedomain.com/bn/products/product-details/72
to:
www.thedomain.com/bn/products/product-details/MR633
I know the rewrite rule needs to be:
RewriteRule ^/bn/products/product-detail/72$ http://www.thedomain.com/bn/products/product-detail/MR633 [R=301]
Will I need to create all 500+ redirect lines manually to add to the htaccess file, or is there a different, better approach? Google has indexed all 500+ pages, so they don't want to lose that juice. Any recommendations would be appreciated.
Also, is it OK/normal to have hundreds of redirects in an .htaccess file?

How do I format this htaccess rule to not redirect a specific query?

I am using an htaccess rule to redirect all urls in the format of:
www.example.com/blog/the-name-of-the-post
to
www.example.com/the-name-of-the-post
So I've removed "blog" from the URL. The redirect rule below is working. However I do not want to redirect any URLs in the format of:
www.example.com/blog/page/x
So if "/page" appears after "blog", then I don't want to do the redirect. The problem is I'm also redirecting the blog pages when paginated.
RedirectMatch 301 ^/blog/([^/]+)/([^/]+)/$ https://www.example.com/$2
In sum,
www.mysite.com/blog/the-name-of-post redirects to www.example.com/the-name-of-post
www.example.com/page/1 (or page/2, page/3, etc) do not redirect
Thanks
If you don't want to redirect when page appears after blog, you can do a negative match for the word page, like so:
RedirectMatch 302 /blog/(?!page).*$ https://www.example.com/
I'm not entirely clear on what all the different possibilities are from your description so I'm just giving you an example of not matching when the word page appears in the second position of the URI.

.htaccess redirect to different domain only if URL is invalid

I am trying to create a .htaccess rule to redirect users from one domain to the other, but this should happen only if the original URL does not exist. For example:
www.domain.com/100 to www.otherdomain.com/PersonalPages/100
www.domain.com/150 to www.otherdomain.com/PersonalPages/150
Now, I don't have a page/file named 100 or 150, so that's why it should redirect, but if user enters "www.domain.com" I do have "index.php" and should stay in "www.domain.com" and not try to go to "www.otherdomain.com/PersonalPages/index.php" (or any other if page exist in my www.domain.com)
I know I can create a simple 301 redirect, but the numbers in the URL are the IDs of users and that number will grow on a daily basis so I can't be adding lines of redirects to my .htaccess for each new user.
Is there a 404 code I could use?
I think this will work for you:
RewriteRule ^([0-9]+)$ http://www.otherdomain.com/PersonalPages/$1
^ and $ mark the beginning/end of the url, [0-9]+ stands for one or more numbers between 0 and 9.

Using .htaccess to redirect only certain non existing url's

I have the following scenario
I have some url's that exist and some url's that do not exist with the following structure
http://site.co.za/component/docman/cat_view/388-documents-reports/test/sss/test/123
http://site.co.za/component/docman/cat_view/388-documents-reports/file/asdsa
http://site.co.za/component/docman/cat_view/388-documents-reports/sadas
http://site.co.za/component/docman/cat_view/388-documents-reports/sadsad
What I'm trying to achieve is when any url that contains component/docman/cat_view does not exist it needs to link to the parent url
for example
http://site.co.za/component/docman/cat_view/388-documents-reports/test/sss/test/123
needs to redirect to
http://site.co.za/component/docman/cat_view/388-documents-reports
http://site.co.za/component/docman/cat_view/388-documents-reports/file/asdsa
needs to redirect to http://site.co.za/component/docman/cat_view/388-documents-reports
so in other words all urls that contain component/docman/cat_view need to redirect to the url plus the first parameter after docman
CAn this be done using the .htaccess file together with mod rewrite and how if it can?
yes using 301 redirects
see below for tutorial
http://www.phatz.com/301redirect.php

Resources