How to redicect url using htaccess in php - .htaccess

I want to redirecy this
from www.example.com/babycare/general_help/category.php?id=12
to
www.example.com/article/category/12`
where id can be dynamic and change from 1 to 100
here is my htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
IndexIgnore *
//this is not working
RewriteCond %{THE_REQUEST} /babycare/general_help/cetegory\.php\?id=([^\s]+) [NC]
RewriteRule ^ /article/category/%1? [L,R]
//this is working
Redirect 301 /babycare/general_help /article
Redirect 301 /babycare/general_help /article
Redirect 301 /babycare/aboutus.php /about
Redirect 301 /babycare/contactus.php /contact
Redirect 301 /babycare/login.php /login
Redirect 301 /babycare/pass.php /pass
Redirect 301 /babycare/registrationform.php /registrationform
Redirect 301 /babycare/termsandconditions.php /termsandconditions
Redirect 301 /babycare/disclaimer.php /disclaimer
Redirect 301 /babycare/your_privacy.php /your_privacy
Redirect 301 /babycare/faq.php /faq
Redirect 301 /babycare/useful-links.php /useful-links
Redirect 301 /babycare/faq.php /faq
Redirect 301 /babycare/whyusethisservice.php /how_can_help
Redirect 301 /babycare/sitemap.php /sitemap
Redirect 301 /babycare/guarantee.php /satisfaction
//this static pages
Redirect 301 /babycare/consultation.php /consultation/first_time
Redirect 301 /babycare/phone_package_detail.php /consultation/support_options
RewriteCond %{THE_REQUEST} /babycare/testimonials\.php\?id=7 [NC]
RewriteRule ^ /testimonial/cat/5? [L,R]
RewriteCond %{THE_REQUEST} /babycare/testimonials\.php\?id=6 [NC]
RewriteRule ^ /testimonial/cat/4? [L,R]
RewriteCond %{THE_REQUEST} /babycare/testimonials\.php\?id=5 [NC]
RewriteRule ^ /testimonial/cat/3? [L,R]
RewriteCond %{THE_REQUEST} /babycare/testimonials\.php\?id=1 [NC]
RewriteRule ^ /testimonial/cat/1? [L,R]
//this is working fine
RewriteCond %{THE_REQUEST} /babycare/testimonials\.php\?id=3 [NC]
RewriteRule ^ /testimonial/cat/2? [L,R]
Redirect 301 /testimonials.php /testimonial

Something like:
RewriteRule ^/article/category/([0-9]+)$ /babycare/general_help/category.php?id=$1 [L]

Use this:
RewriteEngine On
RewriteRule ^article/category/([^/]*)$ /babycare/general_help/category.php?id=$1 [L]
It will give you the URL: www.example.com/article/category/12

i got the error this is now working for me
RewriteCond %{THE_REQUEST} /babycare/general_help/category\.php\?id=([0-9]+) [NC]
RewriteRule ^ /article/category/%1? [L,R]
this redirect me from www.example.com/babycare/general_help/category.php?id=12 to www.example.com/article/category/12

Related

htaccess redirect not working for inner pages / folders

Ok, so I've got a stack of htaccess redirects from one domain to another, which for the most part works fine. However, this particular link isn't working:
http://giccampers.com.au/general-trailers/tipper-trailer/
It should take people to this website:
http://blackseriescampertrailers.com.au/
If you go to the main domain of http://giccampers.com.au/ it redirects fine. But I can't seem to get any of the other pages to redirect from that domain.
Here's what I've got (I didn't set these up, btw):
RewriteCond %{HTTP_HOST} ^giccampers.com.au [NC]
RewriteRule ^(.*)$ http://blackseriescampertrailers.com.au/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.giccampers.com.au [NC]
RewriteRule ^(.*)$ http://blackseriescampertrailers.com.au/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^giccampers\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.giccampers\.com\.au$
RewriteRule ^/?$ "http\:\/\/www\.blackseriescampertrailers\.com\.au\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^www.giccampers.com.au [NC]
RewriteRule ^(.*)$ http://blackseriescampertrailers.com.au/$1 [L,R=301]
redirect 301 /general-trailers/tipper-trailer/ http://blackseriescampertrailers.com.au/
Any help would be great!
use this
RedirectMatch 301 http://giccampers.com.au/general-trailers/tipper-trailer/ http://blackseriescampertrailers.com.au/
RedirectMatch 301 /general-trailers/tipper-trailer/ http://blackseriescampertrailers.com.au/
or
RedirectMatch 301 /tipper-trailer$ http://blackseriescampertrailers.com.au/
or
RedirectMatch 301 /general-trailers/tipper-trailer(.*)http://blackseriescampertrailers.com.au/

Redirect all other pages than the ones specified in my "301 redirect URLs"

I have a list of specific pages I redirect with the "Redirect 301" rule.
Now I'm looking to redirect all the other pages I didn't specificaly mention in this list.
How can I do that?
.htaccess:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)?parachute\-club\-cannes\.com$ [NC]
RewriteRule .* http://parachute-club-cannes.com%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\.parachute-club-cannes\.com$ [NC]
RewriteRule ^ http://parachute-club-cannes.com%{REQUEST_URI} [R=301,L]
# 301 Redirect URLs.
Redirect 301 /www.pccannes.com http://www.parachute-club-cannes.com

301 Redirects not working - Anyone know why?

Can someone help? For some reason the 301 redirects are not working.... The rewrite conditons format my urls by removing the extensions, so not sure if I need to remove the extensions from the 301 statements or not.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.beingchildren\.org$ [NC]
RewriteRule ^(.*) http://www.beingchildren.org/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.htm[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.htm -f
RewriteRule ^(.+?)/?$ /$1.htm [L]
errordocument 404 /404.php
Redirect 301 /Blog/2012-12-25-Christmas-Presents.htm http://www.beingchildren.org/Children-Charity-Blog
Redirect 301 /Blog/2013-03-29-Mixed-Slum-Photos.htm http://www.beingchildren.org/Children-Charity-Blog
Redirect 301 /Blog/2013-05-14-Monthly-Slum-Visits-1.htm http://www.beingchildren.org/Children-Charity-Blog
Redirect 301 /Blog/2013-06-12-Panjim-Monsoon.htm http://www.beingchildren.org/Children-Charity-Blog
Redirect 301 /Blog/2013-06-15-Margao-Charity-Donations-1.htm http://www.beingchildren.org/Children-Charity-Blog
Redirect 301 /Blog/2013-09-08-Margao-Ganesh-Celebrations.htm http://www.beingchildren.org/Children-Charity-Blog
Redirect 301 /Blog/2013-09-08-Margao-Ganesh-Holidays.htm http://www.beingchildren.org/Children-Charity-Blog
Redirect 301 /Charity.htm http://www.beingchildren.org
Redirect 301 /Children-Charity-Pictures.htm http://www.beingchildren.org/Children-Charity-Blog
It is because of 2 issues:
301 rules are at the bottom after .htm is removed
mixing of mod_alias and mod_rewrite rules
Try this code instead:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.beingchildren\.org$ [NC]
RewriteRule ^(.*) http://www.beingchildren.org/$1 [L,R=301]
RewriteRule ^Blog/2012-12-25-Christmas-Presents\.htm$ http://www.beingchildren.org/Children-Charity-Blog [L,R=301,NC]
RewriteRule ^Blog/2013-03-29-Mixed-Slum-Photos\.htm$ http://www.beingchildren.org/Children-Charity-Blog [L,R=301,NC]
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.htm[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.htm -f
RewriteRule ^(.+?)/?$ /$1.htm [L]
PS: I have converted only top 2 of your 301 rules. Once those are working you can similarly convert rest.

Subfolder query string htaccess redirect

Everything I can find for query string redirects talks about something like this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule .? http://mywebsite.com/? [L,R=301]
That's great, but I'm dealing with a url that looks like this:
example.com/portfolio/?gal=16
If I test it without the "portfolio/" it will work fine, but my old site is indexed as above. Any tips on how I can make it work with my URL structure?
EDIT Current .htaccess contents
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
# Redirects
Redirect 301 /feedback http://example.com/testimonials
Redirect 301 /about http://example.com/about-us
Redirect 301 /contact http://example.com/contact-us
#futile attempts at query string redirects omitted
Thanks.
New Edit to include #anubhava's suggestions
Might as well give you the actual .htaccess content since the site is now live.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^dev.bayshorephotography.com$ [OR]
RewriteRule (.*)$ http://bayshorephotography.com/$1 [R=301,L]
# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www.bayshorephotography.com$ [NC]
RewriteRule ^(.*)$ http://bayshorephotography.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^gal=16$
RewriteRule .? http://bayshorephotography.com/portfolio/weddings-02/? [L,R=301]
Redirect 301 /feedback http://bayshorephotography.com/testimonials
Redirect 301 /about http://bayshorephotography.com/about-us
Redirect 301 /contact http://bayshorephotography.com/contact-us
#301 Redirect Entire Directory to remove /blog/ from all blog pages except the main archive itself
RedirectMatch 301 /blog(/.*)/ $1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Old Redirects that don't work
#Redirect 301 /portfolio?gal=20 http://bayshorephotography.com/portfolio-items/weddings-04/
#Redirect 301 /portfolio?gal=29 http://bayshorephotography.com/portfolio-items/our-world-03/
#Redirect 301 /portfolio?gal=16 http://bayshorephotography.com/portfolio-items/weddings-02/
#Redirect 301 /portfolio?gal=18 http://bayshorephotography.com/portfolio-items/weddings-04/
#Redirect 301 /portfolio?gal=28 http://bayshorephotography.com/portfolio-items/album-samples-01/
#Redirect 301 /portfolio?gal=19 http://bayshorephotography.com/portfolio-items/weddings-05/
#Redirect 301 /portfolio?gal=22 http://bayshorephotography.com/portfolio-items/portraits-01/
#Redirect 301 /portfolio?gal=24 http://bayshorephotography.com/portfolio-items/our-world-01/
#Redirect 301 /portfolio?gal=17 http://bayshorephotography.com/portfolio-items/weddings-03/
#Redirect 301 /portfolio?gal=36 http://bayshorephotography.com/portfolio-items/portraits-04/
#Redirect 301 /portfolio?gal=35 http://bayshorephotography.com/portfolio-items/portraits-03/
#Redirect 301 /portfolio?gal=32 http://bayshorephotography.com/portfolio-items/weddings-01/
#Redirect 301 /portfolio?gal=25 http://bayshorephotography.com/portfolio-items/our-world-02/
#Redirect 301 /portfolio?gal=23 http://bayshorephotography.com/portfolio-items/portraits-02/
#Redirect gone /portfolio?gal=34
#Redirect gone /portfolio?gal=33
#Redirect gone /portfolio?gal=27
#Redirect gone /portfolio?gal=21
You need to change order of your rules and keep 301/302 before WP rules:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Canonical Remove www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^view=home$
RewriteRule .? http://example.com/? [L,R=301]
# Redirects
RewriteRule ^feedback/?$ http://example.com/testimonials [L,R=301]
RewriteRule ^about/?$ http://example.com/about-us [L,R=301]
RewriteRule ^contact/?$ http://example.com/contact-us [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Redirect 301 Not working the way I intended it to!

Trying to get a simple 301 redirect with htaccess using this code:
Redirect 301 /cat/radiator-cages/product/radiator-support-cage/ http://www.mysite.com/product/radiator-cages/custom-radiator-support-cage/
The results are sending me to
http://www.mysite.com/product/radiator-cages/custom-radiator-support-cage/?page=cat/radiator-cages/product/radiator-support-cage
Any idea what I'm doing wrong?
Thanks in advance for any help.
--Update--
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteRule ^product/(.*)/(.*)/$ /index.php?page=product&parent_url=$1&product=$2 [L,NC]
Redirect 301 /cat/radiator-cages/product/radiator-support-cage/ http://www.mysite.com/product/radiator-cages/custom-radiator-support-cage/
I suggest trying to use the Redirect 301 statement first.
Your htaccess should then look somthing like this
Redirect 301 /cat/radiator-cages/product/radiator-support-cage/ http://www.mysite.com/product/radiator-cages/custom-radiator-support-cage/
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteRule ^product/(.*)/(.*)/$ /index.php?page=product&parent_url=$1&product=$2 [L,NC]
Edit:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteRule ^/cat/radiator-cages/product/radiator-support-cage/$ http://www.mysite.com/product/radiator-cages/custom-radiator-support-cage/ [R=301,L]
RewriteRule ^product/(.*)/(.*)/$ /index.php?page=product&parent_url=$1&product=$2 [L,NC]
http://www.gerronmulder.com/common-seo-rewrite-rules-for-apache-using-htaccess/

Resources