.htaccess rewrite domainnames, save subdomain and parameters - .htaccess

i have 2 domains but want use only one.
these some examples of domains...
x://test1.domain1.xx/parameter/1
x://test1.domain1.xx/parameter/2
x://test1.domain1.xx/parameter/3
x://test2.domain1.xx/parameter/1
x://test2.domain1.xx/parameter/2
x://test2.domain1.xx/parameter/3
x://test1.domain2.xx/parameter/1
x://test1.domain2.xx/parameter/2
x://test1.domain2.xx/parameter/3
x://test2.domain2.xx/parameter/1
x://test2.domain2.xx/parameter/2
x://test2.domain2.xx/parameter/3
now i want only use xx.domain2.xx/params... all sub/domains with maindomain "domain1" should be rewritten or redirect. without loose parameters or subdomain.
thanks a lot.

I tried sometimes an i think i got a solution.
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain1.xx [NC]
RewriteRule (.*) http://%2.domain2.xx/$1 [R=301,L]

Related

.htaccess redirect folder to subdomain

I've tried applying a few of the answers found on stackoverflow, but either I'm missing something or I'm plain dumb.
Basically I got a main domain name. This domain already has a non-www redirect. So http://domain.com becomes http://www.domain.com. This domain also has a mobile version found inside the the 'm' folder. So accessing the domain name like http://www.domain.com/m/ works and so does http://m.domain.com. What I'm trying to achieve is simple: anyone whom goes to the site via http://www.domain.com/m/, or http://www.domain.com/m/about should be redirected to the subdomain version so to http://m.domain.com or http://m.domain.com/about in the second case listed above.
Whatever I tried implementing ended up with errors, either I managed to disable direct access to m.domain.com, but it worked via domain.com/m/, or redirect loops.
Thanks!
You can use this code in your DOCUMENT_ROOT/.htaccess file of domain.com main .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(domain\.com)$ [NC]
RewriteRule ^m/(.*)$ http://m.%1/$1 [L,NC,R=302]
# non-www to www
RewriteCond %{HTTP_HOST} !^(m|www)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,NC,R=302]

lopping .htaccess condition rule when in aws cluster

I am trying to redirect a link ONLY if it's www.domain to a subdomain admin.domain I have been looking around and I am thinking it's my syntax messing me up. I am not so great with .htaccess redirects and rewrites. Here is what I have used so far.
Example 1
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RedirectMatch 301 ^/wp-admin/?(.*)$ http://admin.mydomain.com/wp-admin/$1
Here is the second on I also have been trying.
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^/wp-admin/(.*)$ http://admin.mydomain.com/wp-admin/$1 [QSA,L,R=301]
This is working off an amazon AWS cluster so the code needs to stop once it rewrites only with www. That is where the loop keeps happening.
Any help would be deeply appreciated!
I think you got it backwards, you want to redirect IF the host is www.mydomain.com, not when it ISN'T. So get rid of the !:
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteRule ^/?wp-admin/(.*)$ http://admin.mydomain.com/wp-admin/$1 [QSA,L,R=301]
Note that the first code block you have won't work at all. You're using a mod_rewrite condition with a mod_alias directive, they're completely independent of each other.

htaccess rule with exception doesn't work properly

I want to redirect all urls under a domain, to another domain, except one or 2 urls.
For example, all urls under domain1.com redirect to domain2.com except the url "domain1.com/cat01"
I wrote this:
RewriteCond %{HTTP_HOST} ^domain1\.com$
RewriteCond %{REQUEST_URI} !^/cat01$
RewriteRule ^(.*)$ http://domain2.com/$1 [R=301,L]
It works in all urls but it redirects "domain1.com/cat01" to "domain2.com/index.php"!
I tried other ways (for example excepting by regex in rewriterule ... but the problem was exsist!
(I use joomla htaccess codes there too)
Thanks Joe
I found another way. I installed ReDJ component on my Joomla,and the problem solved.
but also that way won't work I think:
RewriteCond %{REQUEST_URI} !^/?cat01$
Because I've tried that with and without "/" at start.

How to create virtual username.domain.com using .htaccess mod rewrite

I know there are plenty of questions like I asked, but searching all day the soluation, I didnt find right one that would work for me, therefore I asked question.
Here is .htaccess code I have to view user profiles:
RewriteRule ^users/([^/\.]+)/$ viewProfile.php?user=$1&%{QUERY_STRING}
RewriteRule ^users/([^/\.]+)/([^/\.]+)/$ viewProfile.php?user=$1&usr_profile=$2&%{QUERY_STRING}
And using this rewrite users profile URL is:
http://www.domain.com/users/username.html
Inside of this type of URL, I would like to create subdomain users URL, like:
http://username.domain.com
Can anyone suggest the solution?
Thanks a lot.
Make sure your vhost is setup to accept requests for *.domain.com. Then add this above your other rewrite rules:
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule ^$ viewProfile.php?user=%1 [L,QSA]

.htaccess redirecting dynamic URL

I am using the following code to attempt to redirect a dynamic URL to a new dynamic URL, under the same domain:
RewriteRule ^products/item/^\d([^/]+) /product/$1/ [R=301,L]
I've tried these too:
RewriteRule ^products/item/[^\d]([^/]+) /product/$1/ [R=301,L]
RewriteRule ^products/item/[0-9]([^/]+) /product/$1/ [R=301,L]
But this was redirecting /products/item/342/ to /product/42/, I tested this on a larger number (e.g. 123456789) and it redirected to /product/23456789/.
It would appear that my rule is not picking up the firsts digit, can anyone help me resolve this?
I've also tried using [\d] instaled of [0-9], but this has the same problem.
Cheers!
Try
RewriteRule ^products/item/(\d[^/]+) /product/$1/ [R=301,L]
RewriteRule ^products/item/([0-9]+) /product/$1/ [R=301,L]

Resources