.htaccess redirecting only pages with no subfolder - apache

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]

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.

Redirect 301 dynamic news htaccess

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.

Redirecting all sub-sub pages to another subpage using htaccess

I've a older site running in Apache Server which is already indexed in Google. I wish to redirect all those indexed links to my new site (As the older pages are not existing any more.)
So i wish to redirect all my sub-sub pages to my new root page
I've pages like follows
http://itdost.com/answer-now/Aerobics
http://itdost.com/answer-now/HTML
http://itdost.com/answer-now/Culture
I use the following redirect code for each one
Redirect 301 /answer-now/Engineering http://www.itdost.com/questions/
Redirect 301 /answer-now/Food http://www.itdost.com/questions/
Redirect 301 /answer-now/ASP http://www.itdost.com/questions/
But as the site structure is big, i wish to do it in a single line instead of writing a line for each redirect
Some thing like the following.
Redirect 301 /answer-now/% http://www.itdost.com/questions/
But the above code does not seems to work
In order to use regex better to use mod_rewrite which is more powerful than mod_alias.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^answer-now(/.*|)$ http://www.itdost.com/questions/? [L,NC,R=301]
Try this:
RedirectMatch 301 ^/answer-now/ http://www.itdost.com/questions/