.htaccess rewrite using RedirectMatch - apache

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]

Related

.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).

htaccess redirect subdirectory to root

What rule can I add .htaccess that would give me the following result?
I want to redirect to the homepage any search that includes a certain directory. For example, when anyone tries...
test.com/example
...or...
test.com/example/something
...they end up at...
test.com
Most everything I have tried kinda worked but always ends up appending whatever was after the targeted directory to the domain after the redirect, for example...
test.com/example/something
...leads to...
test.com/something
This is not what I want. Thanks
Code I have tried...
RewriteRule ^example/(.*)$ http://www.test.com [L,R=301]
and
Redirect 301 /example http://www.test.com
UPDATE
This seems to fix it...
RedirectMatch 301 ^/example/ /
Redirect 301 /example http://www.test.com/
Thanks!
Try:
RewriteEngine On
RewriteRule ^example/ / [L,R=301]
or
RedirectMatch 301 ^/example/ /

.htaccess redirect for parent but not children?

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]

Htaccess not working with?

I'm trying to redirect a list of URLs, but any of them that have a ? it isn't working. For example, here's my line of code...
Redirect 301 /calendar-of-events/day/date/2013-03-28/227.html?print=1&tmpl=component https://example.com/index.php/calendar/
This doesn't redirect. However, if I use this...
Redirect 301 /calendar-of-events/day/date/2013-03-28/227.html https://example.com/index.php/calendar/
...or this...
Redirect 301 /calendar-of-events/day/date/2013-03-28/227.html&tmpl=component https://example.com/index.php/calendar/
...and test the URLs out, they redirect correctly.
Any ideas on how to get this working?
You can't match query string using Redirect directive. Use mod_rewrite instead:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=1&tmpl=component$ [NC]
RewriteRule ^calendar-of-events/day/date/2013-03-28/227\.html$ https://example.com/index.php/calendar/? [L,R=301]

Apache redirect

I would like to redirect a URL using RedirectMatch within Apache eg,
/test/one/?? redirect to /test/two/??
where the ?? represents any string that follows
The redirect i'm using below does a straight redirect but doesnt match any string after...
RedirectMatch Permanent ^/test/one?$ /test/two/
Thanks alot
RewriteEngine ON
RewriteBase /
RewriteRule ^/test/one/(.+)$ /test/two/$1
if that does not work, change ^/test/one into ^test/one
make sure mod_rewrite is enabled
You can use mod_rewrite for this:
RewriteEngine On
RewriteBase /
RewriteRule ^/test/one/(.*) /test/two/$1 [L,R=301]
The R flag redirects the page rather than internally rewriting the URI. 301 is the HTTP status code for "Permanently Moved" - if you'd rather use another, you can change it to one of these.