Apache 301 bulk redirect - apache

Is there a way to redirect all link with certain path? For example:
mydomain.com/free-products/product1
mydomain.com/free-products/product2
mydomain.com/free-products/productn
become:
mydomain.com/free/product1
mydomain.com/free/product2
mydomain.com/free/productn
I prefer to redirect using apache vhost instead of htaccess
Thanks for the clues..
Appreciated..

This should work
RewriteEngine on
RewriteRule ^free-products/product([0-9]+)$ /free/product$1 [R=301,L]
There is no matter where to place it, inside vhost or htaccess.

You can also use RedirectMatch.
RedirectMatch permanent ^/free-products/(.*) mydomain.com/free/$1
See http://httpd.apache.org/docs/2.2/mod/mod_alias.html for more information regarding mod_alias.

Related

htaccess redirect only specific page, not subpages

I have a website with following url structure:
www.example.com/products
www.example.com/products/productA
www.example.com/products/productB
I need to redirect www.example.com/products to www.example.com but www.example.com/products/productA and productB should still be available.
Does anyone have an idea?
At the top of your .htaccess file, using mod_rewrite:
RewriteRule ^products$ / [R=302,L]
I've used mod_rewrite here, as opposed to a mod_alias RedirectMatch directive since I assume you are already using mod_rewrite later in the file to rewrite your URLs. It is preferable not to mix redirects/rewrites from both modules.
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule

Redirect after rewrite not working

We have example1.com redirecting to example2.com - this works (also enforces ssl).
But we have a lot of links in the form of example1.com/dir/subdirectory that need to go to example2.com/dir/differentsubdirectory.
Redirect "/skill-catalog" "/catalog"
and
RewriteRule ^/home/skill-catalog/(.*)$ /home/catalog/$1 [R,L]
I have tried each and neither works. What am I doing wrong or am I missing the order they need to be in? Also would this be easier to do in htacess file for wordpress or how I am currently doing it - in the httpd conf file?
Try the following rule:
RewriteRule ^/home/skill-catalog/(.*)$ https://example2.com/home/catalog/$1 [R,L]

.htaccess 301 permanent redirect rule

I have this url structure right now.
http://example.com/weather/in-city_name
which I want to permanently redirect to
http://example.com/city_name/weather
Here is what I'm writing to the .htaccess file using this reference
RedirectMatch ^/weather/(.*)$ http://example.com/$1/weather
But this doesn't work.
In my condition city_name is dynamic and manual entry is not possible.
Any suggestion about how to achieve the desire result would be great.
You can use this .htaccess:
RewriteEngine on
RewriteRule ^weather/in-(.+)/?$ /$1/weather [R=301,NC,L]

htaccess for URLS

Can anyone let me know how i can redirect all urls so they include a new path.
For example, on my old site i have a urls like this:
http://www.example.com/tag/tag-title1/
http://www.example.com/tag/tag-title2/
http://www.example.com/tag/tag-title3/
I now need all of these to be redirected to:
http://www.example.com/waterblog/tag/tag-title1
http://www.example.com/waterblog/tag/tag-title2
http://www.example.com/waterblog/tag/tag-title3
So really all i need to do is add /waterblog/ before the /tag/ on all URLS.
Thanks
You can use this .htaccess:
RewriteEngine on
RewriteRule ^((?!waterblog/).*)$ waterblog/$1 [NC,R,L]
So really all i need to do is add /waterblog/ before the /tag/ on all URLS.
You can use this simple rule in your root .htaccess:
RedirectMatch 302 ^/(tag/.*)$ /waterblog/$1

.htaccess: Redirect depending on accessed url

I want to, in my .htaccess, redirect the user to another url depending on what the user accesses.
In this case, http://example.com/awesome.com and http://awesome.com is the same site, and if the user is accessing http://example.com/awesome.com, I want him or her to be redirected to http://awesome.com.
Is this feasible?
Edit: With the help of answers, I came up with this working solution:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^awesome.com$
RewriteRule ^(.*)$ http://awesome.com/$1 [R=301]
you can use mod_rewrite (apache2 module)
this is the .htaccess that i use in order to redirect from my old domain to my new one (while keeping the link strcture e.g www.domain1.com/link/linkb.html becomes www.domain1.gr/link/linkb.html)
RewriteEngine On
RewriteCond %(www\.)?domain1\.com$ [NC]
RewriteRule .* http://www.domain1.gr%{REQUEST_URI} [R=301,L]
google mod_rewrite for more information (syntax etc)
Not entirely sure about .htaccess, but you could just use server code on your 404 page to redirect them appropriately; this way you could collect stats, setup a toolbar, or whatever other actions you might want to take.
.htaccess is about authorization, not redirection. I recommend you look at the redirection support for Apache (or whatever web server you're using), which is a much better fit for this problem and just make sure your .htaccess/authorization is in line with the target.
This rule should do it:
RewriteRule ^awesome\.example(/.*)?$ http://www.awesome.example$1 [R=301,L]
Check the Redirect & RedirectMatch options in apache. For simple cases, like yours it's simplier than a mod_rewrite.
Redirect /awesome.com http://ww.awesome.com
or
Redirect permanent /awesome.com http://ww.awesome.com
Now, if example.com and awesome.com are on the same apache server and same virtualhost you're maybe mising the named bases virtualhost things and you're maybe trying to make something really more complex than a simple named base virtualhost definition.