htaccess rule with exception doesn't work properly - .htaccess

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.

Related

Subdomain url redirect to a URL

We have about 50 URLs that need to be redirected, but subdomain names may differ, for example:
http://abc.OLDdomain.com > http://cba.NEWdomain.com
http://foo.OLDdomain.com > http://bar.NEWdomainc.com
Most of the answers suggest rules, but in our case source / target subdomains often differ, so it's not a straightforward redirect rule that could solve it.
Also, can you do this type of redirect without rewrite rules?
So ideally something like
redirect 301 abc.OLDdomain.com http://cba.NEWdomain.com
Which does not currently work
I'm asking if there's a way to do it without rewrite rules because the htaccess is managed by someone who's not tech savvy but understands simple redirect directives such as the one above.
My current working solution with rewriterule/rewritecond is:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc.OLDdomain.com$ [NC]
RewriteRule ^(.*)$ http://cba.NEWdomain.com%{REQUEST_URI} [R=302,NC,L,QSA]
RewriteCond %{HTTP_HOST} ^foo.OLDdomain.com$ [NC]
RewriteRule ^(.*)$ http://bar.NEWdomainc.com%{REQUEST_URI} [R=302,NC,L,QSA]
If there's a one liner alternative without "RewriteCond and RewriteRule" that would be great

.htaccess wrongly redirects to non-existent site

I have two domains. One of them contains a latin character, and is thus an IDN url. What I want is to redirect both the regular domain to the IDN domain, and to remove the www prefix from both domain names.
My .htaccess file looks like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^regularurl\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.regularurl\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.xn--idnurl-ujb\.com$ [NC]
RewriteRule ^(.*)$ http://xn--idnurl-ujb.com/$1 [R=301,L]
The problem is - it redirects correctly to xn--idnurl-ujb.com, but in any case (no matter if what url I redirect from - with or without www) appends www to the end, so that it becomes like this: xn--idnurl-ujb.com/www. This is clearly not what I intended, as that url is non-existent and redirects to the 404 site. I can't understand why this happens - I've tried many different code variations, too. I've successfully done some .htaccess redirects in the past, but they weren't IDN domain names - does that have anything to do with it? What am I doing wrong?
Also, if there is anything I could do to make the code easier or more dynamic, please let me know.

.htaccess rewrite domainnames, save subdomain and parameters

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]

.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]

URL Rewriting for url with countrycode

I am writing a multi language website. Therefore I would like some help with a URL rewrite problem.
Case:
When someone visits www.example.com without adding a country code (nl, en, de) the htaccess redirects the visitor to www.example.com/nl/ i.g.
RewriteRule !(nl|en|de)(.*).* /nl/ [R=301,L]
The website is renewed and has got many url's directing to the website (google, forums). i.g. www.example.com/oldpage-nomore.html. What I would like is the following; the htaccess should detect that the request uri doesn't contain nl,en or de and should redirect to pagenotfound.php. RewriteRule ^(.*)\.html /public/oldurl?section=nl&notfound=$1$2&basehref=true&%1 [PT,L] the problem with this Rewrite rule is: all files ending in .html are being redirected.
What I am looking for is the following:
When someone visits www.example.com and no request uri is entered this should redirect to www.example.com/nl/
When there is a requested uri and this doens't contain a countrycode (nl|en|de) than redirect to pagenotfound.php
I tried the following but it doens't work:
RewriteCond %{REQUEST_URI} !^(nl|en|de)$
RewriteRule ^([a-z]{2})/(.*)\.html$ /pagenotfound.php?page=$2 [L,R=404]
I hope someone can help.
Thank you in advance.
Try to place this .htaccess file at the server root folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^.*$ /nl/ [R]
RewriteCond %{REQUEST_URI} !^/(nl|en|de)
RewriteRule ^.*/(.*)\.html$ /pagenotfound.php?page=$2 [L,R=404]
(you probably made several mistakes, I tried to fix them)
I'm not sure with the first RewriteCond %{REQUEST_URI} regular expression - maybe remove the question mark or slash, I don't now... can't test now.

Resources