Conditional redirects in .htaccess - .htaccess

I'm not sure if what I want to achieve is even possible via .htaccess, I have trawled Google without any luck.
I have a pile of redirects to apply to a multi-store website which means that two domains will share the same .htaccess file. The problem that I have is that the redirects for each domain are conflicting with each other.
Is it possible set up the equivalent of a php 'if' statement within the .htaccess file without adding a condition before each redirect?
<If domain = http://www.example-1.com>
RedirectMatch 301 ^/en/1-2-drive-torque http://www.example-1.com/90-britool-expert-torque-wrenches
RedirectMatch 301 ^/en/10-Britool http://www.example-1.com/24-britool-expert-spanners
RedirectMatch 301 ^/en/11-Britool http://www.example-1.com/78-britool-expert-combination-spanners
RedirectMatch 301 ^/en/12-Britool http://www.example-1.com/77-britool-expert-ratchet-spanners
RedirectMatch 301 ^/en/13-Britool http://www.example-1.com/79-britool-expert-open-end-spanners
etc...
</If>
<If domain = http://www.example-2.com>
RedirectMatch 301 ^/en/1-2-drive-torque http://www.example-2.com/390-britool-expert-torque-wrenches
RedirectMatch 301 ^/en/10-Britool http://www.example-2.com/624-britool-expert-spanners
RedirectMatch 301 ^/en/11-Britool http://www.example-2.com/478-britool-expert-combination-spanners
RedirectMatch 301 ^/en/12-Britool http://www.example-2.com/677-britool-expert-ratchet-spanners
RedirectMatch 301 ^/en/13-Britool http://www.example-2.com/779-britool-expert-open-end-spanners
etc...
</If>
Any help or advice will be very much appreciated

You'll need to use mod_rewrite for this instead and use the %{HTTP_HOST}. Unfortunately, there's no way to create "if" containers like you have in your example. You'll need to duplicate the condition each time:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/1-2-drive-torque /90-britool-expert-torque-wrenches [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/10-Britool /24-britool-expert-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/11-Britool /78-britool-expert-combination-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/12-Britool /77-britool-expert-ratchet-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-1\.com$ [NC]
RewriteRule ^en/13-Britool /79-britool-expert-open-end-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/1-2-drive-torque /390-britool-expert-torque-wrenches [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/10-Britool /624-britool-expert-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/11-Britool /478-britool-expert-combination-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/12-Britool /677-britool-expert-ratchet-spanners [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example-2\.com$ [NC]
RewriteRule ^en/13-Britool /779-britool-expert-open-end-spanners [L,R=301]

Related

301 Redirect to new domain with some specific URLs

I saw similar topics but couldn't find a practical answer to my problem.
I'm moving my old website to a new one, and some URLs are changing.
I would like to make a generic 301 redirection to the new domain (because most paths are the same), while individually redirecting some URLs.
Here is what I have on my old website .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule (.*)$ https://new.com/$1 [R=301,L]
Redirect 301 "/custom/url/" "https://new.com/my-custom-url"
</IfModule>
But the 301 redirects to : https://new.com/custom/url instead of https://new.com/my-custom-url
Some of my URLs also have URL parameters I would like to redirect, such as :
Redirect 301 "/brand.php?name=Example" "https://new.com/Example"
Redirect 301 "/brand.php?name=Example2" "https://new.com/another/url"
which do not seem to work as well.
Thank you very much for your help.
But the 301 redirects to : https://new.com/custom/url instead of https://new.com/my-custom-url
It is because your specific redirect rule appears after generic one. Moreover you are mixing mod_rewrite rules with mod_alias rules and these are invoked at different times.
Have it like this:
RewriteEngine On
# redirect /brand.php?name=Example2 to new.com/another/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example2) [NC]
RewriteRule ^brand\.php$ https://new.com/another/%1? [R=301,L,NE]
# redirect /brand.php?name=Example3 to new.com/category/Example3
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example3) [NC]
RewriteRule ^brand\.php$ https://new.com/category/%1? [R=301,L,NE]
# generic redirect /brand.php?name=Example to new.com/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC]
RewriteRule ^brand\.php$ https://new.com/%1? [R=301,L,NE]
# redirect custom URL
RewriteRule ^custom/url/ https://new.com/my-custom-url [R=301,L,NE,NC]
# redirect everything else
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteRule ^ https://new.com%{REQUEST_URI} [R=301,L]

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/

Htaccess redirects from digits at the end

I want to make redirects from urls like
/test/112321 to /test/
/test/test2/1311223 /test/test2/
There are only digits in the end of the url.
Now i have
RewriteCond %{HTTP_HOST} ^(.*)([0-9]*)/$ [NC]
RewriteRule ^(.*)/([0-9]*)$ http://%1/$1/ [R=301,L]
but it doesn't work.
Could you help me with this?
You don't need RewriteCond %{HTTP_HOST} as you're matching REQUEST_URI which you can do in RewriteRule itself.
You can use this rule in site root .htaccess:
RewriteEngine On
RewriteRule ^(.+)/([0-9]+)/?$ /$1/ [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(.*)/([0-9]+)/?$
rather than:
RewriteCond %{HTTP_HOST} ^(.*)([0-9]*)/$
You can also use RedirectMatch
RedirectMatch 301 ^/(.+)/([0-9]+)/?$ /$1

301 .htaccess redirect with queries just after /

I've been struggling to do 301 redirects, and none of the existing topics helps.
I need to make redirects with .htaccess for the following pages:
Redirect 301 http://www.mypage.com/?q=company/contacts http://www.mypage.com/contacts
Redirect 301 http://www.mypage.com/?q=product/new/ghz/name-5 http://www.mypage.com/name-5
I know that I should use rewrite rules and specify {QUERY-STRING} and believe me, I've tried. Nothing helps.
Matching against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?q=company/contacts
RewriteRule ^$ http://www.mypage.com/contacts? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?q=product/new/ghz/name-5
RewriteRule ^$ http://www.mypage.com/name-5? [R=301,L]
Or the query string (this matches rewritten URIs)
RewriteCond %{QUERY_STRING} ^q=company/contacts$
RewriteRule ^$ http://www.mypage.com/contacts? [R=301,L]
RewriteCond %{QUERY_STRING} ^q=product/new/ghz/name-5$
RewriteRule ^$ http://www.mypage.com/name-5? [R=301,L]

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