htaccess rewrite only part of the querystring - .htaccess

I am trying to perform the following rule in htaccess:
www.domain.com/folder/?id=14077&c=en-gb -> www.domain.com/folder/?id=14077
www.domain.com/folder/?c=en-gb&ID=14077 -> www.domain.com/folder/?id=14077
www.domain.com/folder/?id=14077&c=fr-fr -> www.domain.fr/folder/?id=14077
www.domain.com/folder2/?c=fr-fr&ID=14077 -> www.domain.fr/folder2/?id=14077
www.domain.com/folder2/?c=en-us&ID=14077 -> www.domain.us/folder2/?id=14077
Basically take out the "c" part of the querystring and redirect it to a new domain, based on the following rules:
c=en-gb -> www.domain.com
c=fr-fr -> www.domain.fr
c=en-us -> www.domain.us
Any help welcome!

Admittedly, I'm not sure if this is a task best solved by mod_rewrite...but what the hell, why not:
(Not fully tested, but it seems to work well)
RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)(\A|&)c=([A-Za-z\-]+)&?(&.*)?$
RewriteRule .* - [E=SWITCHLANG:%3,E=QSONE:%1,E=QSTWO:%4]
RewriteCond %{ENV:SWITCHLANG} =en-gb [NC]
RewriteCond %{HTTP_HOST} !(.*)\.com$
RewriteRule (.*) http://www.domain.com/$1?%{ENV:QSONE}&%{ENV:QSTWO}
RewriteCond %{ENV:SWITCHLANG} =fr-fr [NC]
RewriteCond %{HTTP_HOST} !(.*)\.fr$
RewriteRule (.*) http://www.domain.fr/$1?%{ENV:QSONE}&%{ENV:QSTWO}
RewriteCond %{ENV:SWITCHLANG} =en-us [NC]
RewriteCond %{HTTP_HOST} !(.*)\.us$
RewriteRule (.*) http://www.domain.us/$1?%{ENV:QSONE}&%{ENV:QSTWO}

Related

301 Redirect to remove subdomain works on and off

Here is what I want to do:
travel.xx.com => xx.com/travel
travel.xx.com/fun => xx.com/fun
travel.xx.com/page.htm => xx.com/page.htm
health.xx.com => xx.com/health
health.xx.com/food => xx.com/food
health.xx.com/page2.htm => xx.com/page2.htm
Here is my htaccess rules:
RULE SET NUM 1:
//to convert subdomain.xx.com to xx.com/subdomain and subdomain.xx.com/WHATEVER to xx.com/subdomain/WHATEVER
RewriteCond %{HTTP_HOST} ^health.xx.com$ [NC]
RewriteRule ^(.*)$ http://xx.com/health/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^travel.xx.com$ [NC]
RewriteRule ^(.*)$ http://xx.com/travel/$1 [R=301,L]
RULE SET NUM 2:
to convert xx.com/subdomain/WHATEVER to xx.com/WHATEVER
RewriteRule ^(travel|health)(\/)([a-zA-Z0-9\&^\-\_\!\:\,\.\']+)?$ $3 [R=301,L]
Rule set number one and number two work fine separately. When I put them together, rule number 2 stops working and redirects to the root.
Can anyone tell me what I am missing here or give me a solution for this?
Thanks,
Instead of 3 rules shown you just need these 2 rules doing same thing:
RewriteCond %{HTTP_HOST} ^(health|travel)\.(xx\.com)$ [NC]
RewriteRule ^/?$ http://%2/%1/ [R=302,L,NE]
RewriteCond %{HTTP_HOST} ^(health|travel)\.(xx\.com)$ [NC]
RewriteRule ^(.+)$ http://%2/$1 [R=302,L,NE]
Make sure to keep this code before other rules.

htaccess redirection subdomain

I want to redirect feedback.domain.de to www.domain.de/de/abc/cde.html via htaccess.
My current htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.de$
RewriteRule ^(.*)$ http://www.domain.de/$1 [L,R=301]
SO, I thought just to add:
redirect 301 feedback.domain.de www.domain.de/de/abc/cde.html
but it doesn't work. If i try to open feedback.domain.de it redirects to www.domain.de
I know this is a very easy question but I don't how solve it in htaccess :-(
The result I want is:
domain.de -> www.domain.de/de/index.html
www.domain.de -> www.domain.de/de/index.html
domain.de/de/example.html -> www.domain.de/de/example.html
etc...
feedback.domain.de -> www.domain.de/de/feed.html
Best regards
The Redirect directive only accept a path relative to the root path (for example /my-path) and won't match on the host part of the URL.
Try this (note that here I assume you want to redirect domain.de to www.domain.de and not all subdomains):
RewriteEngine On
# Redirect feedback.domain.de to http://www.domain.de/de/abc/cde.html
RewriteCond %{HTTP_HOST} ^feedback\.domain\.de$
RewriteRule . http://www.domain.de/de/abc/cde.html [L,R=301]
# Redirect domain.de to www.domain.de
RewriteCond %{HTTP_HOST} ^domain\.de$
RewriteRule . http://www.domain.de/$0 [L,R=301]
Updated answer (2013-03-18):
# Redirect feedback.domain.de to http://www.domain.de/de/abc/cde.html
RewriteCond %{HTTP_HOST} ^feedback\.(domain\.(de))$
RewriteRule .+ http://www.%1/%2/feed.html [L,R=301]
# domain.de -> www.domain.de/de
RewriteCond %{HTTP_HOST} ^(domain\.(de))$
RewriteRule .+ http://www.%1/%2/$0 [L,R=301]
# www.domain.de/de -> www.domain.de/de/index.html
RewriteCond %{HTTP_HOST} ^www\.domain\.(de)$
RewriteRule ^%1/?$ index.html [L,R=301]

Multi-site force www. on ALL subdomains htaccess snippet

I previously got some great help here with a tad bit complicated .htaccess file.
It is a multi-site/domain file and uses %{HTTP_HOST} to rewrite/301 the non-wwww to the www.widgets.com address.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The problem is that RegexFu only looks for the NOT www. and then adds the www.
This could cause some duplicate content issues if someone links to say, mail.widgets.com as this is redirected to www.mail.widgets.com.
To summarize:
I need:
mail.widgets.com -> www.widgets.com
dogs.widgets.com -> www.widgets.com
www.mail.widgets.com -> www.widgets.com
www.dogs.widgets.com -> www.widgets.com
etc.
I have tried a variety of permutations liek this with no luck:
RewriteCond %{HTTP_HOST} !^www\.[^\.]+\.com [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
No dice. I either end up with a loop or some other problem.
I need simply anything that isn't:
www. (anything but period) .com - > www. (last string before .com) .com
and of coarse the standard:
(anything but period).com -> www. (anything but period).com
If someone could lend me a hand I would greatly appreciate it.
Try:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.[^.]+\.[^.]+ [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^(.*)$ http://www.%1.com/$1 [R=301,L]
The %1 backreferences the grouping that matches ([^.]+) in the previous rewrite condition. It ignores any subdomain that's before the domain name, if there is one.

HtAccess Rewrite works with [R] but fails with [L]

So, I am trying to make: sub.example.com/page rewrite to www.example.com/sub/page
I have this code which does work (note last character):
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*) http://www.example.com/%1/$1 [R]
And this code that does not:
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*) http://www.example.com/%1/$1 [L]
I want the url to remain as the user typed it in, but this seems to produce a 500 error. I've enabled logging to try to assist, but it doesn't provide any clues. Is this possible?
Try changing the last Rule to
RewriteRule ^(.*) http://www.example.com/%1/$1 [P]
If you list your directory structure to show how sub.example.com and www.example.com are related, this may also be possible via a rewrite vs the more expensive (slower) proxy solution above.

multiple redirects with .htaccess

I want to do this
website.com/blog/index.php -> website.com/blog
website.com/admin/archief_login.php -> website.com/admin
this works with my code.
but I want to add this:
website.com/aa -> website.com/web/index.php?init=aa
for some reason the blog gets this redirect: website.com/blog/?init=blog
what is the best way to set these different rewrites?
RewriteEngine on Options All -Indexes
RewriteCond %{HTTP_HOST}
^websit.com$ [OR] RewriteCond
%{HTTP_HOST} ^www.website.com$
RewriteRule ^admin$
"http\:\/\/www.website.com\/admin/archief_login.php"
[R=301,L]
RewriteRule ^blog$
"http\:\/\/www.website.com\/blog/index.php"
[R=301,L]
DirectoryIndex client_login.php
RewriteRule
^screen-([a-zA-Z0-9_-]+).html$
index_client.php?screen=$1
RewriteRule
^invoice([a-zA-Z0-9_-]+).html$
make_invoice.php?id=$1
RewriteRule
^pack-([a-zA-Z0-9_-]+).html$
index_client.php?screen=pack_code&wwwcode=$1
You need to put the more "general" rules lower in the file so they don't match almost all of your URLs
RewriteRule ^(\w)$ /web/index.php?init=$1 [L, NC]
RewriteRule ^blog$ /blog/index.php [R=301,L]
The above will do
website.com/aa => website.com/web/index.php?init=aa
website.com/blog => website.com/web/index.php?init=blog
If you reverse the two rules you will get
website.com/aa => website.com/web/index.php?init=aa
website.com/blog => website.com/blog/index.php

Resources