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

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

Related

htaccess HTTPS redirect failing

I have two separate queries on my vhost.conf file but both are failing and here is the code example;
1: I want to be able to get anything from this domain and check whether its http OR https and then redirect it to the rewrite rule
RewriteCond %{HTTP_HOST} ^(www\.example\.com|example\.com|example2\.example\.com) [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^/testing$ https://google.co.uk [R=301,L,QSA]
For some odd reason, it only works on HTTP protocols, so the redirects are working fine on http://example.com/testing. However, i need it to also work for the https version.
2: I have some RedirectMatch 301 in place to redirect catch all on all domains. However, none of them seem to be working. Question: is that the best method or should use RewriteRule.
RedirectMatch 301 ^(/superexample/?.*)$ http://www.superexample.com
RedirectMatch 301 ^/enexample(/?.*)$ https://www.enexample.com

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

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

Apache redirect with GET variables

I would like to redirect all requests to a subfolder on down on a site. So:
http://thesite.com/oldfolder/whatever/anything.php?getvar_stuff
goes to
http://thesite.com/newfolder/
If I use the following .htaccess file:
RedirectMatch 301 /oldfolder http://thesite.com/newfolder/
.. the URL that returns is:
http://thesite.com/newfolder/?getvar_stuff
Don't want the "post_stuff", so I change the rewrite line to:
RedirectMatch 301 /oldfolder http://thesite.com/newfolder/?
.. the URL that returns is:
http://thesite.com/newfolder/?
Which is better, but I'd still love to lose that question mark. Is it possible?
It appears that the best way to do this is not using Redirect, but rather Rewrite:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/oldfolder/
RewriteRule ^(.*)$ http://thesite.com/newfolder/? [R=301,L]

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.