Redirect 301 dynamic news htaccess - apache

I changed the URL structure for a website. Now I want to create a 301 rule in the .htaccess so if anyone is using the old URL via google for example it will be redirected to the homepage.
The old structure was /category/[article name] and sometimes /category/sub-category/[article name] now if use the following rule
Redirect 301 /category/ http://example.com/
It is only working when I type in /category/ wich is logic. So I tryed
Redirect 301 /category/(.*) http://example.com/
This is not taking effect. So my question is how can I redirect every URL when an URL is entered like my example above. So it doesn't matter what comes after /catagory/ it needs to be redirected so google will remove my URL's from the index and will index the new ones with the new sitemap I uploaded.
Thanks

You can't use Redirect like that, you would need to use RedirectMatch but sometimes it's problematic.
If you want to have more control and power over your redirects I would suggest using mod_rewrite.
RewriteEngine On
RewriteRule ^category/(.*)/?$ http://example.com/ [R=301,L]
This will get you what you want and it always works for me.

Related

.htaccess 301 redirect page specific + wildcard the rest

The site I'm working has moved to a new domain, with a completely different site structure and file naming.
I've set up a .htaccess file to re-direct important pages to the equivalent pages on the new domain:
RewriteEngine On
Redirect 301 /oldpage1/ http://www.newdomain.org/newpage1
Redirect 301 /folder/oldpage2/ http://www.newdomain.org/newpage2
Redirect 301 /oldpage3/ http://www.newdomain.org/folder/another-newpage
...
...and so on.
Now, I want to catch all other URLs under the old domain and direct them to the homepage of the new site. So something like:
Redirect 301 /*/ http://www.newdomain.org
But how is this really done?
Thank you for any help!
Upfront, RewriteEngine On is not necessary with Redirect directives, because RewriteEngine is part of mod_rewrite, and Redirect belongs to mod_alias. These are two different ways of achieving a similar goal.
Now, just to answer the question, you may use RedirectMatch for this
RedirectMatch / http://www.newdomain.org
This will redirect any path from the old domain to the home page of the new domain.
Please note, that the Redirect directive uses the "old" URL as a prefix
Then 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.
This means
Redirect /oldpage1/ http://www.newdomain.org/newpage1
will redirect all URLs, that start with /oldpage1/, not just oldpage1 alone.
And finally, never test with 301!

Redirect with .htaccess to exact URL provided

So I want to redirect from old sites url to new one. Lets say example.com/en/some/stuff/foo/bar needs to be redirected to example.com/some/stuff.
Here is what I have at he moment:
Redirect 301 /en/some/stuff/foo/bar/ /some/stuff/
The problem is that I end up being redirected here example.com/some/stuff/foo/bar, but I need as I defined inside .htaccess example.com/some/stuff.
How to redirect properly to exact URL I have provided without anything extra.
You can use this redirect rule with regex in your site root .htaccess:
RewriteEngine On
RewriteRule ^en/(some/stuff)/.+ /$1 [L,NC,R=301]
Make sure to test it in a new browser to avoid old cache.

301 redirect urls with a slash in querystring

We built a new webshop for one of our clients, and are 301 redirecting their old url's to our normal ones. As usual we do this using .htaccess as follows:
Redirect 301 /url1/ http://www.url2.com/ and it works fine.
BUT the old shop has this querystring with slashes in it (!) , for example:
/epages/14353.sf/de_DE/?ObjectPath=/Shops/61922345/Products/32428
And as soon as I use this in a htaccess 301 redirect string it stops working. I don't get a 500 error or something like that, but when I visit /epages/14353.sf/de_DE/?ObjectPath=/Shops/61922345/Products/32428 it won't redirect. If I would use it with a querystring like the following (/epages/14353.sf/de_DE/?ObjectPath=foobar) I can visit it and get redirected, but not if there's a slash in it.
I tried backslashing it, encoding etc. but without the right results. Does any1 have an idea? I tried AllowEncodedSlashes On but it gave me a 500 error.
Since you cannot match QUERY_STRING in Redirect directive, you need to use mod_rewrite based rule.
Have this one in your root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ObjectPath=/Shops/61922345/Products/32428
RewriteRule ^/?epages/14353\.sf/de_DE/?$ https://www.nu.nl? [L,NC,R=301]
Make sure to test this in a new browser to avoid old cached data.

.htaccess redirecting only pages with no subfolder

I am using .htaccess to redirect pages from an old website to a new site. The URL structure has changed completely, but thanks to a SEO person I have a complete list of URLs I have to redirect and take care of.
My .htaccess looks like the following:
Options +FollowSymlinks
RewriteEngine On
Redirect 301 /some-guy http://www.new.domain/kontakt/
Redirect 301 /some-other-guy http://www.new.domain/kontakt/
...
Redirect 301 /de/some-page http://www.new.domain/some/subpage/
Redirect 301 /de/another/page http://www.new.domain/nice-page/
Of course the URLs can be even longer and contain query strings and stuff like that. The new website has a different URL structure and does not need query strings and nothing.
Now, while the first two redirects work perfectly, every redirect which contains a subfolder fails to redirect to the target location. The last URL for example redirects to
http://www.new.domain/de/another/page
What am I missing here?
Try
Redirect 301 ^de/another/page http://www.new.domain/nice-page [L]
I got it working now with little modifications to Anup Saunds code (and therefore using mod_rewrite).
I used the following structure which seems to work fine with all links I have and sets the status code for search engines.
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^what/ever/your/path-is(.*)$ http://www.new.domain/oh-what/a-nice-path/ [R=301,nc]

Redirect URL with a parameter to homepage

Recently we have released a new website for a company but some of the old links still apear on google webmaster tools.
I tried to create a redirect with some luck, however I want to fully remove the parameter and redirect all the links matching the structure to the new homepage.
So these pages
http://www.example.com/job/view.php?id=142
http://www.example.com/job/view.php?id=3453
http://www.example.com/job/view.php?id=567
will redirect to
http://www.example.com
I used this redirect
RedirectMatch 301 /job/view.php$ http://www.example.com/
which works OK with the exception that the resulting website looks like:
http://www.example.com/?id=142
You can try this way
RewriteEngine on
RewriteRule ^job/view\.php$ http://www.example.com/? [R=301,L]