htaccess redirect specific URL to different domain - .htaccess

I have 2 domain pointing to same directory, Now I want to redirect some URLs to one specific domain and rest should be on main domain.
I have one domain www.xyz.com this is main domain site will be open with this domain, from this domain I want to redirect specific URLs to different domain so if someone open www.xyz.com/a/theatre then it should redirect to www.abc.com/a/theatre. And now from same this new domain if someone click on URL which don't have "/a/theatre" in URL then it should redirect back to main domain. So if someone open www.abc.com/a/classes then it should redirect to www.xyz.com/a/classes
I have used bellow rules but not working,
RewriteCond %{HTTP_HOST} ^www.xyz.com$ [NC]
RewriteCond %{REQUEST_URI} ^/a/theatre$
RewriteRule (.*)$ http://www.abc.com/a/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]
RewriteCond %{REQUEST_URI} !^/a/theatre
RewriteRule (.*)$ http://www.xyz.com/a/$1 [NC,L,R=301]
Any help is appreciated.
Thanks!
Here is all code from my htaccess, I have installed Joomla on this domain.
Options -Indexes
RewriteEngine on
RewriteBase /seo
RewriteCond %{HTTP_HOST} ^www.mcleancenter.org$ [NC]
RewriteCond %{REQUEST_URI} ^/seo/alden-theatre$
RewriteRule ^/?seo/(.*)$ http://www.aldentheatre.org/seo/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.aldentheatre.org$ [NC]
RewriteCond %{REQUEST_URI} !^/seo/alden-theatre
RewriteRule ^/?seo/(.*)$ http://www.mcleancenter.org/seo/$1 [NC,L,R=301]
########## Begin - Joomla! core SEF Section
#
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
#
########## End - Joomla! core SEF Section

You need to match out the leading /a/ by adding a ^/?a/ to the front of the regex in the rules.
RewriteCond %{HTTP_HOST} ^www.xyz.com$ [NC]
RewriteCond %{REQUEST_URI} ^/a/theatre$
RewriteRule ^/?a/(.*)$ http://www.abc.com/a/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]
RewriteCond %{REQUEST_URI} !^/a/theatre
RewriteRule ^/?a/(.*)$ http://www.xyz.com/a/$1 [NC,L,R=301]

Related

HTACCESS redirect controller folder to subdomain

I am stuck at a problem with .HTACCESS FILE.
I am using CodeIgniter 3
Scenario:
We have a domain https://example.com and the admin panel is in a folder like
application/controllers/manage/<controllers here>
Now I need this to happen.
If someone goes to https://example.com/manage should be redirected to
https://admin.example.com/manage.
Keep in mind that both subdomain & main domain points to same directory.
Also I want this to happen:
If someone goes to any url like
https://admin.example.com/<anyhting-other-than-manage>
to redirect to
https://example.com/<anything-other-than-manage>
Here is my current .htaccess
# gtranslate config
RewriteRule ^(af|sq|am|ar|hy|az|eu|be|bn|bs|bg|ca|ceb|ny|zh-CN|zh-TW|co|hr|cs|da|nl|en|eo|et|tl|fi|fr|fy|gl|ka|de|el|gu|ht|ha|haw|iw|hi|hmn|hu|is|ig|id|ga|it|ja|jw|kn|kk|km|ko|ku|ky|lo|la|lv|lt|lb|mk|mg|ms|ml|mt|mi|mr|mn|my|ne|no|ps|fa|pl|pt|pa|ro|ru|sm|gd|sr|st|sn|sd|si|sk|sl|so|es|su|sw|sv|tg|ta|te|th|tr|uk|ur|uz|vi|cy|xh|yi|yo|zu)/(.*)$ /gtranslate/gtranslate.php?glang=$1&gurl=$2 [L,QSA]
RewriteRule ^(af|sq|am|ar|hy|az|eu|be|bn|bs|bg|ca|ceb|ny|zh-CN|zh-TW|co|hr|cs|da|nl|en|eo|et|tl|fi|fr|fy|gl|ka|de|el|gu|ht|ha|haw|iw|hi|hmn|hu|is|ig|id|ga|it|ja|jw|kn|kk|km|ko|ku|ky|lo|la|lv|lt|lb|mk|mg|ms|ml|mt|mi|mr|mn|my|ne|no|ps|fa|pl|pt|pa|ro|ru|sm|gd|sr|st|sn|sd|si|sk|sl|so|es|su|sw|sv|tg|ta|te|th|tr|uk|ur|uz|vi|cy|xh|yi|yo|zu)$ /$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^static.example.com [NC]
RewriteRule !^(assets|uploads)/ https://example.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^admin.example.com [NC]
RewriteRule !^(manage)/ https://example.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(manage)/ https://admin.example.com/manage%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# procedure was miss spelled
Redirect 301 /yellwo /yellow
# Redirect 301 ......
Please help
UPDATE: Following does seems to work for redirecting admin.example.com<NOT-MANAGE-URI> to example.com/<NOT-MANAGE-URI>
<IfModule mod_rewrite.c>
RewriteEngine On
#gtranslate code
# ........
RewriteCond %{HTTP_HOST} ^static.example.com [NC]
RewriteRule !^(assets|uploads)/ https://example.com%{REQUEST_URI} [R,L]
# REDIRECT ADMIN
RewriteCond %{HTTP_HOST} ^admin.example.com [NC]
RewriteCond %{REQUEST_URI} !^/manage/
RewriteRule (.*) https://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
.......
After this:
URL in browser https://admin.example.com/about-us redirects to https://example.com/about-us which is correct.
but writing https://admin.example.com/manage/ redirects to https://example.com/index.php/manage/
You can insert this rule at top of your .htaccess to redirect example.com/manage to admin.example.com/manage:
RewriteCond %{HTTP_HOST} ^(?:www\.)?.(example\.com)$ [NC]
RewriteRule ^manage(?:/.*)?$ https://admin.%1%{REQUEST_URI} [NC,R=301,NE,L]

Magento redirection with htaccess

I want to redirect example.com to example.au,but at the same time i want to exclude example.com/admin from redirection. Is it possible if yes please provide any rewrite rules.Any help would be appreciated.
Code:
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
#RewriteRule .* index.php [L]
RewriteRule ^(.*)$ www.example.com.au/$1 [R=301,L]
RewriteRule !^(admin/.*)$ www.example.com.au/$1 [R=301,L]
You can use following .htaccess redirection code for access admin
GeoIPEnable On
ReWriteEngine On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} IN$ //IN is the country code where you want to redirect
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_URI} !admin
RewriteCond %{REQUEST_URI} !index.php [NC] //admin keywords
RewriteRule ^ http://www.domain.in [R=301,L] //redirection url
In the above example admin and index.php are the keywords for accessing admin
RewriteCond %{REQUEST_URI} !admin
RewriteCond %{REQUEST_URI} !index.php [NC]
It means redirect to specific url in INDIA,only when the current url doesn't have the above keywords(all admin modules only have these keywords).
It would be help you. I'm also using the above same code for my store
All the best
EDIT : You can refer the following threads too for getting some more details
Some admin modules redirect to frontend
Magento redirection with htaccess based on country wise

Redirect some pages to HTTPS

First I directed my /example folder to main domain. Now, I'd like to redirect some of the pages to HTTPS. Pls, anyone could advice me about the code:
# com upload yonlendirme
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/example/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ example/index.php [L]
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#redirect www.mydomain.com to mydomain.com (or any other subdomain)
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
#force https for certain pages
RewriteCond %{HTTPS} !=on
RewriteRule ^(index\.php?route=account/register|index\.php?route=account/login|index\.php?route=account/account|index\.php?route=checkout/checkout)$ https://%{HTTP_HOST}%{REQUEST_URI}[L,R]
You can't match against the query string (everything after the ?) in a rewrite rule. You'll need to match against the %{QUERY_STRING} variable in a rewrite condition:
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} ^route=(account/register|account/login|account/account|checkout/checkout)
RewriteRule ^index.php$ https://%{HTTP_HOST}%{REQUEST_URI}[L,R]
You may also want to move that rule up to the top of your htaccess file so the routing to the example folder doesn't interfere with the redirects.

Excluding specific pages from HTTPS in mod_rewrite

I am trying to setup a codeigniter app to force HTTPS across all pages except one. However, I cannot get the rules to only redirect if the user is not on the page in question.
The page that should be excluded has the following URL's:
http://mydomain.com/kpi/reports/67
http://mydomain.com/kpi/reports/67/overview/2013-02-01/2013-02-28
http://mydomain.com/index.php?/kpi/reports/67
http://mydomain.com/index.php?/kpi/reports/67/overview/2013-02-01/2013-02-28
The number 67 and the dates can all change in the URL's above hence the user of regular expressions below.
I have tested the regular expressions and they seem to match the URL's fine. However, the htaccess just seems to redirect it to https:// anyway.
My .htaccess file is as follows...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Disallow access to system dir
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Disallow access to application dir
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Force https when not on overview report
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/index\.php\?/kpi/reports/?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/index\.php\?/kpi/reports/?([0-9]+)/overview/?([0-9]+)-?([0-9]+)-?([0-9]+)/?([0-9]+)-?([0-9]+)-?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/kpi/reports/?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/kpi/reports/?([0-9]+)/overview/?([0-9]+)-?([0-9]+)-?([0-9]+)/?([0-9]+)-?([0-9]+)-?([0-9]+)$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]
#If not a valid file, redirect request through index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Any help would be greatly appreciated!
Maybe this will do what you need:
#Force https when not on overview report
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !kpi/reports/[0-9]+/?$ [NC]
RewriteCond %{QUERY_STRING} !kpi/reports/[0-9]+/overview/[^/]+/[^/]+/? [NC]
RewriteCond %{REQUEST_URI} !kpi/reports/[0-9]+/?$ [NC]
RewriteCond %{REQUEST_URI} !kpi/reports/[0-9]+/overview/[^/]+/[^/]+/? [NC]
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]
#If not a valid file, redirect request through index.php
Replace all lines between the comments.

https in htaccess and order of rules (using Expression Engine)

I'm building a site in expression engine that part of needs to be https. The site is also using a new domain name (new one www.example-new.com the old one www.example.old.com).
I want to do the following things:
remove the index.php
force www
force https for any url starting www.example.old.com/extranet
redirect https URLs that are not www.example.old.com/extranet (e.g. www.example.old.com/news to http
I have the following code so far that works for the first two requirements:
<IfModule mod_rewrite.c>
RewriteEngine On
# Force www
RewriteCond %{HTTP_HOST} ^example-new.com$ [NC]
RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
AddType x-httpd-php53 .php
I seem to be going round in circles, so I've two questions that will help me write the other rewrites (although feel free to share suggestions...):
1) Should the code for requirements 3 and 4 be positioned before "removes index.php" code?
2) Does the position have any bearing on the redirects that will be coming from the old site e.g. www.example-old.com/some-link-here.asp will be redirected to www.example-new.com/some-new-link-here
Thanks,
Gregor
1) Remove 'index.php' from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
2) Add 'www' to all URIs
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
3) Force https:// for any URI starting with /extranet
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/extranet(.*)$ [NC]
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
4) Redirect https:// URIs that are not /extranet
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/extranet(.*)$ [NC]
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
Putting it all together, here's your complete set of RewriteRules:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/extranet(.*)$ [NC]
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/extranet(.*)$ [NC]
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
1) Should the code for requirements 3 and 4 be positioned before "removes index.php" code?
The rules are processed in the order that you write them. I expect that you want the site to redirect first then do the remove index, so yes, it should go before
2) Does the position have any bearing on the redirects that will be coming from the old site e.g. www.example-old.com/some-link-here.asp will be redirected to www.example-new.com/some-new-link-here
If you are using the same directory for both sites, then you would need to prefix all rules for one site with a RewriteCond that limits the domain. Otherwise, order of the rules (old vs new site) is important.
Your redirects from the old site should also incorporate rules in the new e.g. ensure that you are going to http/s as necessary to avoid extra redirects.
Below is the code to force http/s
#if not secure
RewriteCond %{HTTPS} off
#and starts with /extranet
RewriteCond %{REQUEST_URI} ^/extranet [NC]
#redirect to secure
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#if secure
RewriteCond %{HTTPS} on
#and does not start with /extranet
RewriteCond %{REQUEST_URI} !^/extranet [NC]
#redirect to http
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force www on hostname, but keep same protocol (http/https)
RewriteCond %{HTTP_HOST} !^www\.(.+)
RewriteCond %{HTTPS}s ^(on(s)|offs)
RewriteRule ^ http%2://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If it helps... I just did this yesterday for an ecommerce site... Here's my .htaccess file
# Remove WWW from URL
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteCond %{REQUEST_METHOD} !=POST
RewriteRule ^(.*)$ $1/ [L,R=301]
#remove index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
#Force HTTPS on checkout/account pages
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (checkout|account)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#remove HTTPS on all other pages
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(img|_|images|checkout|account)
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Resources