.htaccess Url Redirect Issues - apache

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

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

Directory Redirect to sub-folder with same name

I am trying to redirect directory (.htaccess) to subdirectory with same name. I am getting redirection loop error.
RedirectMatch 301 /abc http://www.example.com/lite/abc/
You must delimit your pattern otherwise it will match abc everytime.
With RedirectMatch
RedirectMatch 301 ^/abc.*$ http://www.example.com/lite/abc/
Or with mod_rewrite
RewriteEngine On
RewriteRule ^abc.*$ /lite/abc/ [R=301,L]
Note: you'll need to clear your browser's cache before trying this code. Actually, your old rule is now in cache.

Website article .htaccess 301 redirect

I have a site in a folder called patients and my urls look like so:
http://site.com/patients/post-name
http://site.com/patients/articles/another-post-name
I want to redirect them all to a clean:
http://newsite.com/post-name
http://newsite.com/another-post-name
In other words, lose the /articles/ which sometimes appears and 301 to the new site.
Any help on how to do this with htaccess?
Edit your .htaccess in http://site.com server and put this(must be the first rule):
RewriteEngine On
RewriteRule .* http://newsite.com/ [R=301]
or
Redirect 301 / http://newsite.com/
Regards.

Simple 301 redirect in .htaccess with query string does not work with Redirect directive

I am trying to redirect a single URL in a .htaccess file with Redirect:
Redirect 301 /index2.php?option=com_rss&feed=RSS2.0&no_html=1 /something/somethingelse/
I have a bunch of other similar rules which work using directory structure URLs, but this one refuses to get processed.
Redirect 301 /old/url/ /new/url/
Do I have to do anything special?
Thanks!
With Redirect you can only test for URL paths, or more specifically, URL path prefixes but not for the URL query. But you can do so with mod_rewrite:
RewriteEngine on
RewriteCond %{QUERY_STRING} =option=com_rss&feed=RSS2.0&no_html=1
RewriteRule ^index2\.php$ /something/somethingelse/? [L,R=301]