Apache Redirect - same domain different REQUEST_URI - apache

I need multiple RewriteCond for the same domain, but different REQUEST_URIs
I need to redirect to a different domain depending on the REQUEST_URI.
I am trying this with no luck:
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/myuir/ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*)$ / [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/myuniqueuri/ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*)$ http://example2.com/$! [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/myotheruniqueuri/ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*)$ http://example3.com/$! [R=301,L]
I thought the [OR] on the request uri condition would work, but it did not. I have also tried without the [OR] and it did not work!
For example:
if the url is:
http://example.com/myuniqueuri
needs to redirect to:
http://example2.com
if the url is:
http://example.com/myotheruniqueuri
needs to redirect to:
http://example3.com
where example2 and example3 can be anything but are not the same.
Any help is greatly appreciated
Thanks

I think the problem might be in your syntax. The reference is $1 not $!. Also the ! is being used incorrectly. This basically means not. Like if not this directory then etc. Well it appears you want to match the directory and then redirect based on that. Try this code and see if it works for you.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/myuir
RewriteRule ^(.*)$ / [R=301,L]
RewriteCond %{REQUEST_URI} ^/myuniqueuri
RewriteRule ^(.*)$ http://example2.com/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/myotheruniqueuri
RewriteRule ^(.*)$ http://example3.com/ [R=301,L]

Related

Apache - .HTACCESS applies previous conditions to current rewrite url

My .htaccess applies RewriteCond's from previous RewriteRule's to the current RewriteRule.
For example example.com/acme gets redirected to http://www.example.com/index.php?url=acme.
Why would this configuration ever redirect to index.php? The index.php's RewriteRule isn't even a R=301. And the RewriteCond's that makes exceptions when the request_uri contains the string acme shouldn't even be applying themselves to this RewriteRule since they have their own RewriteRule.
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(txt) [OR]
RewriteCond %{REQUEST_URI} robots\.txt
RewriteCond %{REQUEST_URI} !acme
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !\.(txt) [OR]
RewriteCond %{REQUEST_URI} robots\.txt
RewriteCond %{HTTPS} !=off
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
It seems lke it executed 2 rewrite rules at the same time. Adding the [END] flag fixed the issue.

Using htaccess to redirect all pages but one to another domain

My htaccess is as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.es$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.es$
RewriteRule ^(.*)$ http://www.domain.com/es [L,R=301]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?section=$1 [QSA,L]
Basically it's function is to redirect all domain alias (i.e. domain.mobi) to domain.com, except domain.es, that will be redirected to domain.com/es. Then there's another rewriterule that appends the query string (so in the background, domain.com/test becomes domain.com/index.php?section=test)
My problem is that now I need to exclude a url (www.domain.es/landing) from the redirection (so it stays in domain.es), and I can't make it work. I've tried adding this condition to the first two rules to exclude the page:
RewriteCond %{REQUEST_URI} !^/landing$
but then it goes to www.domain.com/es?section=landing (not only still redirects to domain.com; the query string appears in the browser bar). Any ideas?
You can use these rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.es$ [NC]
RewriteCond %{THE_REQUEST} !/landing[\s?] [NC]
RewriteRule ^(.*)$ http://www.domain.com/es/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{THE_REQUEST} !/landing[\s?] [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?section=$1 [QSA,L]

Redirect to new url except for on page with existing expression engine redirect

I have the following redirects in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} /content [NC]
RewriteRule (.*) http://subdomain.domain1.co.uk/index.php/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/content$ [NC]
RewriteRule (.*) http://www.domain2.co.uk/$1 [R=301,L]
The first redierect needs to be there for the expression engine to work properly. Visit to
subdomain.domain1.co.uk/content matches the rule but I end up in the indefinite redirect loop. Can anybody help ?
Thanks,
EDIT: 2nd and 3rd rule may actually be incorrect. What I want it to do is to redirect anything from http://subdomain.domain1.co.uk/$1 to http://www.domain2.co.uk/$1 expect for http://subdomain.domain1.co.uk/content and http://subdomain.domain1.co.uk/content/*
Keep your rules like this:
RewriteEngine on
# redirect everything except content/? to www.domain2.co.uk
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain1\.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !\s/content/?[\s?] [NC]
RewriteRule ^ http://www.domain2.co.uk%{REQUEST_URI} [R=301,L,NE,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]

redirect with .htaccess rewrite rule to different folder on my server

I have been trying to redirect to a different folder with .htaccess when I hit a domain.
Here's my .htaccess redirect rule, can you tell me where am I wrong?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
So, what I want is, if someone visits racereadymotorsports.in, they redirect to /raceready/ directory
and on the other hand if they visit to annelies-slabbynck.com, they should redirect to /annelies/ directory
At present, this redirects twice for annelies-slabbynck.com which means that, I end up with:
http://www.annelies-slabbynck.com/annelies/annelies/ as my final url
I am running a shared hosting and do not have access to add a new configuration.
Please help.
You need to remove the [OR] flag and reproduce the same conditions with the other rule. Maybe something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
Or better yet, you can use these conditions instead: RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteCond %{DOCUMENT_ROOT}/raceready/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/raceready/$1 -d
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteCond %{DOCUMENT_ROOT}/annelies/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/annelies/$1 -d
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
Note that the R=301 flag in your second rule redirects the browser, unlike the first rule which only internally rewrites the request.

Mod_rewrite not redirecting all cases correctly

I always seem to have trouble with this and no matter what I try I can't get all cases to redirect correctly.
I currently have
example.com/anything
correctly redirects to
example.com/track.php?memb=anything
and basically any url with a filetype at the end should ignore all redirects. I also have a subdomain rule at the top which I think is implemented correctly to not interfere with what I am trying to add. All of this is currently working correctly with the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)? [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)/? [NC]
RewriteRule .* track.php?memb=%1 [L,E=END]
I need to add the following rule:
example.com/c/whatever
needs to redirect to
example.com/page2.php?c=whatever
After many attempts and modifications of some of the current rules, I can't seem to get it working but I think I'm close with
RewriteRule ^c/(.*)$ page2.php?c=$1 [L,E=END]
RewriteCond is only valid for the next RewriteRule. Also,
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)? [NC]
will match anything with a . in the name. Since you have a ? at the end, it won't matter if the extension has a valid name or not.
Try this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)$ [NC]
RewriteRule ^c/([^/]+)/? page2.php?c=$1 [L,E=END]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)$ [NC]
RewriteRule ^([^/]+)/? track.php?memb=$1 [L,E=END]