Need htaccess redirection - apache

I want to redirect below urls using htaccess
http://abc.domain.com to http://www.domain.com/pos
http://xyz.domain.com/weighing-scales.php to http://www.domain.com/weighing
I tried using below directive in .htaccess file but it is not working. Kindly help
Redirect 301 http://abc.domain.com http://www.domain.com/pos
Redirect 301 http://xyz.domain.com/weighing-scales.php http://www.domain.com/weighing-scales

You can use the following :
RewriteEngine on
#--abc.domain.com/ => domain.com/pos--#
RewriteCond %{HTTP_HOST} ^abc\.domain\.com$ [NC]
RewriteRule ^$ http://domain.com/pos [L,R]
#--xyz.domain.com/weighing-scales\.php =>domain.com/weighing--#
RewriteCond %{HTTP_HOST} ^xyz\.domain\.com$ [NC]
RewriteRule ^weighing-scales\.php$ http://domain.com/weighing [L,R]

Related

htaccess redirection - stop redirecting subdomain

I have this code in my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
RedirectMatch 301 ^/de/(.*)$ http://de.example.com/$1
My problem is that it redirects even http://xyz.example.com/de/ to http://de.example.com/
What I need is still redirecting from example.com/de to de.example.com
but without redirecting xyz.example.com/de to de.example.com
Could you help me to modify that?
You ar mixing directives from 2 different Apache modules mod_rewrite and mod_alias. Stick to mod_rewrite only and have this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^de/(.*)$ http://de.example.com/$1 [L,NC,R=302]

Redirecting Subdirectory to Subdomain with either htaccess or cpanel

I want to redirect a subdirectory to a subdomain either using .htaccess or cpanel
redirects from
domain.in/subfolder
to
subfolder.domain.in
i m using cakephp app for both domain.in and subfolder.domain.in
each have separate core library
Please help me to solve this
Thanks in advance
You can use this rule as very first rule in your /subfolder/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.in$ [NC]
RewriteRule (.*) http://subfolder.domain.in/$1 [NE,R=302,L]
Or else:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(domain\.in)$ [NC]
RewriteRule (.*) http://subfolder.%1/$1 [NE,R=302,L]
Using the following .htaccess file might work:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.in [NC]
RewriteRule ^(.*)$ http://www.domain.in$1 [L,R=301]
RedirectMatch 301 ^/subfolder/(.*)$ http://subfolder.domain.in/$1

How to fix 301 redirect that is not working

I'm trying to create a redirect from https://www.compareking.no/penger/kredittkort to https://www.compareking.no/forbrukslaan but nothing is happening when I modify the htacess file.
This is the rewrite rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.compareking.no/penger/kredittkort$ [NC]
RewriteRule ^(.*)$ http:// www.compareking.no/forbrukslaan/$1 [L,R=301]
Thoughts?
HTTP_HOST only matches the HOST/domain part of the URL, that specified in RewriteCond and the URI prefix in RewriteRule would get you on the right track, as shown below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.compareking\.no$ [NC]
RewriteRule ^penger/kredittkort(.*)$ http://www.compareking.no/forbrukslaan$1 [L,R=301]

htaccess Redirect a Specific Directory URLs to another different URL?

With .htaccess how to redirect from:
http://apple.example.com/old_folder
http://apple.example.com/old_folder/
http://apple.example.com/old_folder/bla/bla/bla?a=b&etc
whatever depths under apple.example.com/old_folder --> to:
http://banana.example.com/new_folder/
.. only?
I used following code, but not working:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^apple.example.com/old_folder$
RewriteRule ^(.*)$ http://banana.example.com/new_folder/ [R,L]
You cannot match URI in RewriteCond %{HTTP_HOST} condition.
This rule should work from root .htaccess of apple.example.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^apple\.example\.com$ [NC]
RewriteRule ^old_folder(/|$) http://banana.example.com/new_folder/? [R,L]
EDIT: OR else use this rule in /old_folder/.htaccess:
RewriteEngine on
RewriteRule ^ http://banana.example.com/new_folder/? [R,L]

301 Redirect using .htaccess does not work

Here is my rule in .htaccess file:
Redirect 301 /George-Nelson-Bench-CT3005-EDI6.htm?categoryId=-1 http://www.mydomain.com/proddetail.php?prod=George_Nelson_Bench
But this is showing a 404 error on my website.
Some other code on the .htaccess file is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
Some other 301 redirection which works properly:
Redirect 301 /Modern-Classics_c8.htm http://www.mydomain.com/categories.php?cat=10
Redirect 301 /Sofas_c34.htm http://www.mydomain.com/products.php?cat=25
Redirect 301 /Bedroom_c2.htm http://www.mydomain.com/categories.php?cat=7
So, why the first 301 redirect rule is not working?Any suggestions?
Since you're anyway using mod_rewrite its better to replace mod_alias based code to mod_rewrite which is more powerful and flexible.
You first Redirect rule isn't working because you're using a query parameter. Replace that rule with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+George-Nelson-Bench-CT3005-EDI6\.htm\?categoryId=-1\s [NC]
RewriteRule ^ /proddetail.php?prod=George_Nelson_Bench? [R=301,L]