.htaccess redirect for parent but not children? - apache

I have a number of URLs that I need to redirect to new locations, but there are some situations where child pages need to remain active and not redirected. For example:
/products would redirect to http://www.newsite.com/products
/products/category1 would redirect to http://www.newsite/products/category1
But /products/specialitem would not get redirected at all.
Is this possible with either Redirect or RedirectMatch?
Doing a Redirect 301 /products http://www.newsite.com/products seems to affect all child pages
Thanks for any guidance!
Edit:
Using waynethec's answer, I was able to get started. But can anyone clarify why my first rule below works but the others do not?
RedirectMatch 301 ^segment-one$ http://www.google.com/
RedirectMatch 301 ^segment-one/segment-two$ http://news.google.com/
RedirectMatch 301 ^segment-one/segment-two/segment-three$ http://cnn.com/
RedirectMatch 301 ^segment-one/segment-two/segment-three/foobar$ http://gbv.com/
(By not working, I mean that I still can get to the pages, rather than them getting redirected.)

You should be able to use the following RedirectMatch rule:
RedirectMatch 301 ^/products$ http://www.newsite.com/products
Note that this will only redirect requests for /products, not /products/, or /products/pagename.extension.

You can use RedirectMatch:
RedirectMatch 301 ^/products(?!/specialitem)(.*)$ http://www.newsite.com/products$1
This will redirect /products, or anything after it, except /products/specialitem/

If you need to add conditions, this also worked for me. Note, I had to eliminate the slash between ^ and products when using RewriteRule.
RewriteCond [...whatever your conditions are...]
# test with 302 first to avoid annoying caching issues
# RewriteRule ^products$ http://www.newsite.com/products [R=302,NC,L]
# but use 301 in production once you know it's working
RewriteRule ^products$ http://www.newsite.com/products [R=301,NC,L]

Related

Alternative for bulk Redirect 301 in htaccess

I have some link redirects in htaccess in which every link should be added with /en after .com i.e.,
mydomain.com, mydomain.com/abc.html
to
mydomain.com/en, mydomain.com/en/abc.html
After some research i found that redirect 301 does it. But I think this is not perfect and permanent solution. In future if I add another URL, again I have to add it to htaccess. So can any one help me out in getting the perfect and permanent solution for this.
I have done this in my htaccess
Redirect 301 /about.html /en/about.html
Redirect 301 /contact-us.html /en/contact-us.html
Redirect 301 /index.html /en/index.html
Redirect 301 /locations.html /en/locations.html
You can use this single redirect rule in your site root .htaccess:
RewriteEngine On
RewriteRule ^(?!en/)(.+\.html)$ /en/$1 [L,NC,NE,R=301]
(?!en/) is negative lookahead to assert that URI is not already starting with /en/.

.htaccess RedirectMatch getting 404 for example.com/choice/

I am trying to do a .htaccess redirect and I have it working, except for one of my cases.
When I have this rule:
RedirectMatch "^/choice$" "/choice/home.html"
The address: www.example.com/choice redirects to www.example.com/choice/home.html.
In contrast when I have this rule:
RedirectMatch "^/choice/$" "/choice/home.html"
And give enter the address: www.example.com/choice/
I get a 404.
I tried this modification of the rule:
RedirectMatch "^/choice\/$" "/choice/home.html"
Recycled the server, but I'm still getting 404.
This is the condition I have set:
RewriteCond %{REQUEST_URI} ^/choice/*
My goal is to have it redirect to the same page whether the final / is present in the address or not. Anyone have any insight? Thanks!
RedirectMatch and RewriteCond have nothing to do with one another. The first is part of mod_alias and the second is part of mod_rewrite - these are two separate modules.
You can accomplish your redirect by using the following:
RewriteEngine on
RewriteRule ^choice/?$ /choice/home.html [R=302,L]
Change 302 to 301 to make the redirect permanent (cached).

Apache Conditional RedirectMatch

I have moved a website from /blog/ to the web root. I created a RedirectMatch 301 for my URLs which I believe is correct:
RewriteEngine On
RewriteBase /
RedirectMatch 301 ^/blog/(.*)$ /$1
This works correctly. Now, domain.tld/blog/foo/ redirects to domain.tld/foo/
However, there is one issue. There is a page on the website with the slug: /blog/
Now you can't access the page since domain.tld/blog/ redirects to domain.tld/
So what I would like to accomplish is not to redirect /blog/, but to redirect /blog/everything/else/
domain.tld/blog/ no redirection
domain.tld/blog/foo/ redirects to domain.tld/foo/
domain.tld/blog/foo/bar/ redirects to domain.tld/foo/bar/
Etc.
Many thanks for your help!
Can you try this? I think it needs a little change:
RewriteEngine On
RewriteBase /
RedirectMatch 301 ^/blog/(.+)$ /$1
Remember to clear cache + history on browsers before testing, browsers remember 301 redirect rules.
Hope that helps.

.htaccess Url Redirect Issues

I have two folders on my domain that i have renamed. I wanted to add some redirection scripts into my .htaccess file so that users with the old links get to the right location. I tried using using;
RedirectMatch 301 /oldfolder/subsection-01/(.*) example.com/new-folder/subsection/$1
RedirectMatch 301 /oldfolder/(.*) example.com/new-folder/$1
and they both worked! However all my image links broke, because their file paths on the server have the following path:
example.com/sites/default/files/**oldfolder**/filename.jpg
I Have been looking for a way to either exclude references to .jpg files or ensure that only the folders with that include the domain uri e.g. 'example.com/oldfolder' are acted upon but to no avail.
I tried the following techniques;
RedirectMatch 301 %{HTTP_HOST} example\.com/oldfolder/subsection-01/(.*) example.com/new-folder/subsection/$1
RedirectMatch 301 example.com/oldfolder/(.*) example.com/new-folder/$1
RewriteRule %{HTTP_HOST}.example\.com/oldfolder/subsection-01/$ example.com/new-folder/subsection/$1 [L]
RewriteRule %{HTTP_HOST} ^www\.example\.com/oldfolder/(.*) example.com/new-folder/$1 [R=301,L]
But they've either resulted in server 501 errors or done nothing at all.
I Would appreciate any useful assistance the community can provide.
Thanks.
Use start anchor in your regex to make sure your your pattern is matched only when it is at start:
RedirectMatch 301 ^/oldfolder/subsection-01/(.*) http://example.com/new-folder/subsection/$1
RedirectMatch 301 ^/oldfolder/(.*) http://example.com/new-folder/$1

.htaccess rewrite using RedirectMatch

I need to redirect several URL to one
FROM
1.example.com/groups/3d/
2.example.com/groups/3d/skype:softlist_ua?chat
3.example.com/groups/3d/+/
4.example.com/groups/3d/+/+/
5.example.com/groups/3d/+/skype:softlist_ua?chat
TO
example.com/catalog/3d/
Redirect statement works fine, however when old URL containts get reuqests, it adds this get request to end of the new URL. How can I remove it?
RedirectMatch 301 ^/groups/3d/.*$ /catalog/3d/$1
Try:
RedirectMatch 301 ^/groups/3d/.*$ /catalog/3d/?
or
RewriteEngine On
RewriteRUle ^groups/3d/ /catalog/3d/? [L,R=301]