How do I setup an .htaccess file to exclude a subdirectory? - .htaccess

I currently have a website setup in the following way:
example.com is the root domain
The .htaccess file redirects all requests to the root URL to blog.example.com
This is because the blog subdomain is a mirror of the main domain and is what will be used moving forward.
The problem is that there's a second domain: product.com that is pointing to example.com/product.
As the .htaccess file is configured, all incoming requests are mapped to blog.example.com.
This means that going to product.com will result in blog.example.com/product in which the ideal scenario is to have example.com/product and all other incoming requests be redirected to blog.example.com.
A copy of my .htaccess file is below:
# Block access to the root site
RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
# Whitelist specific areas of the root site
# e.g category slugs or page slugs you want to remain viewable
RewriteCond %{REQUEST_URI} !/blog.* [NC]
RewriteCond %{THE_REQUEST} !/blog.* [NC]
# Set like-to-like redirect URL for anything not whitelisted
RewriteRule (.*) http://blog\.example\.com/$1 [R=301,L]
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
How can I construct my rules in the following way:
product.com redirects to example.com/product
example.com redirects to blog.example.com

This should work:
RewriteEngine On
# redirect anything that is different than /blog and /product to blog.example.com
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !/blog.* [NC]
RewriteCond %{REQUEST_URI} !/product.* [NC]
RewriteRule ^(.*)$ http://blog.example.com/$1 [R=301,L]

Related

Redirect old domain and pages to equivalent pages on new domain

I am trying to redirect an old domain to a new domain. The pages have the same names e.g. /my-amazing-page, however on the new website, the url would be /se/my-amazing-page.
So for example, I need a rule that redirects
olddomain.se/recipes/[ALL RECIPE PAGES]
to
newdomain.com/se/recipes/[ALL RECIPE PAGES]
I've tried many of the answers here on Stack Overflow as well as the solutions described here but I just can't get any of them to work. All they seem to do is redirect the old domain to the home page (root) of the new domain, and ignore the sub directory and pages.
Currently I am using the following:
RewriteCond %{HTTP_HOST} ^olddomain\.com/recipes/$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/se/recipes/$1 [R=301,L]
However as mentioned, this just redirects me to the home page of the new domain. And yes, I have cleared my browser cache and am testing in incognito mode in my browser.
My current HTaccess file in its entirety:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
# END WordPress
RewriteCond %{HTTP_HOST} ^olddomain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/recipes/
RewriteRule ^recipes/(.*)$ https://newdomain.com/se/recipes/$1 [R=301,L]
/recipes is a url path not host.
You need to use ℅{REQUEST_URI} . to match against url path. The following rule should work .
RewriteCond %{HTTP_HOST} ^olddomain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/recipes/
RewriteRule ^recipes/(.*)$ https://newdomain.com/se/recipes/$1 [R=301,L]
The second RewriteCond is optional in this rule . You can remove because the RewriteRule already takes care of url path starting with /recipes .

301 redirect not working in .htaccess

I have two domains, autodromodifranciacorta.it and franciacortacircuit.com both pointing to the same website hosted on this IP address: 94.23.64.40.
Now i want all the content to be under one single domain, so i decided to 301 redirect all the traffic from franciacortacircuit.com to autodromodifranciacorta.it
Here is my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^franciacortacircuit\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.autodromodifranciacorta\.it$ [NC]
RewriteRule ^(.*)$ http://autodromodifranciacorta.it/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The redirect is not working and i have no clue why, because the syntax looks correct to me.
The .htaccess file is processed, because if i put a typo in it, i get server error.
Wha'ts wrong with it?
Your second http_host condition is wrong and it never matches if the current host is franciacortacircuit.com, You need to use an OR condition to match against 2 diffrent hosts.
#Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^franciacortacircuit\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.autodromodifranciacorta\.it$ [NC]
RewriteRule ^(.*)$ http://autodromodifranciacorta.it/$1 [R=301,L]

Force redirect to https by htaccess

I have domain and a subdomain like example.com and blog.example.com and enabled ssl for both using wildcard on bluehost. I add following code suggested by bluehost
Custom subdomain .htaccess SSL + WordPress
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.maindomain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^subdomain.maindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
# End custom subdomain .htaccess
# Custom maindomain .htaccess WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?maindomain.com$
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP_HOST} ^(www.)?maindomain.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# End custom maindomain .htaccess
sites are running on https properly but they are accessible on http as well.
i tried following code to force redirect but its not working.
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
also i have two htaccess file for main domain and for subdomain and above code is placed in main domain htaccess file.
am i doing something wrong or missing something to force all urls to open with https:// ?
Have you tried this :
For only domain
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
For subdomain(place .htaccess inside subdomain folder)
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://blog.example.com/$1 [R,L]

htaccess redirect not working as thought

Ok so i have an admin folder in a site that i am working and what i need is when someone enters
http://admin.teamfocususa.org/
in their browser then they get redirected to the admin folder
here is my htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^teamfocususa.org [NC]
RewriteRule ^(.*)$ http://www.teamfocususa.org$1 [L,R=301]
RedirectMatch 301 ^/admin/(.*)$ http://admin.teamfocususa.org/$1
Redirect http://admin.teamfocususa.org /admin
this part is not working
Redirect http://admin.teamfocususa.org /admin
any ideas on how to make this part work ...when i visit http://admin.teamfocususa.org is the home page and not the admin folder as i thought....FYI this is a shared host and I dont have access to the vhost to fix this the way i know
Try adding a RewriteRule matching on requests to / of admin.teamfocususa.org.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^teamfocususa\.org^ [NC]
RewriteRule ^(.*)$ http://www.teamfocususa.org$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?teamfocususa\.org [NC]
RewriteRule ^admin(.*) http://admin.teamfocususa.org/$1 [L,R=301]
# Not needed
#RedirectMatch 301 ^/admin/(.*)$ http://admin.teamfocususa.org/$1
# Added = requests to http://admin.teamfocususa.org/
# Redirected into http://admin.teamfocususa.org/admin
# --OOPS fixed RewriteCond - was typo RewriteRule
RewriteCond %{HTTP_HOST} ^admin\.teamfocususa\.org$ [NC]
RewriteRule ^$ admin/ [L,R=301]
# To hide /admin on admin.teamfocususa.org
# use this instead of the above group...
RewriteCond %{HTTP_HOST} ^admin\.teamfocususa\.org$ [NC]
RewriteRule ^$ admin/ [L]
# Not needed
#Redirect http://admin.teamfocususa.org /admin
I do not think you want to do a Redirect, but a simple RewriteRule to show the admin folder for the domain admin.teamfocususa.org
RewriteEngine On
RewriteBase /
# If not admin.teamfocususa.org and in /admin folder, redirect to admin.teamfocususa.or
RewriteCond %{HTTP_HOST} ^admin.teamfocususa.org [NC]
RewriteRule ^/admin/(.*)$ http://admin.teamfocususa.org/$1 [L,R=301]
# If domain is admin.teamfocususa.org, show files from admin folder
RewriteCond %{HTTP_HOST} admin.teamfocususa.org [NC]
RewriteRule (.*)$ /admin$1 [L]
# if domain is teamfocususa.org, redirect to www.teamfocususa.org
RewriteCond %{HTTP_HOST} ^teamfocususa.org [NC]
RewriteRule ^(.*)$ http://www.teamfocususa.org$1 [L,R=301]
UPDATE: My domain checks were wrong, try this updated version

htaccess subdir to subdomain and subdomain to subdir redirect problem

I'm trying to accomplish the following:
http:// www.example.com/site/abc with a http 301 redirect to subdomain http:// abc.example.com
and back again within Apache:
http:// abc.example.com --> /site/abc
I want both redirects to be defined in the .htaccess in the root folder.
I've tried several combinations, but unfortunately without any luck. This is what I have now:
# 1. redirect uris which start with www. to the domain without www.
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]
# 2. rewrite http://host/site/<name>/<uri> => http://<name>.host/<uri>
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{REQUEST_URI} ^/site/([^/]+)
RewriteRule ^(.*) http://%1.example.com/$1 [R=301,NC,L]
# 3. internal redirect to the corresponding directory
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ site/%1/ [L,NC]
I receive a 500 server error instead.
Thanks in advance!
Just to clarify in your original question you said you need this redirection:
http://www.example.com/site/abc => http://abc.example.com/site/abc (**site/abc also present** in destination URL)
But later in your comment you suggested:
http://www.example.com/site/abc/xyz/part?id=123&name=lmn => http://abc.example.com/xyz/part?id=123&name=lmn (**site/abc missing** from destination URL)
Assuming your comment are right, please try this in your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule site/(.+)/(.*) http://$1.example.com/$2 [R=301,L]
This will redirect www.example.com/site/foo/bar* to foo.example.com/bar* with 301 status to the browser.
Assuming /site/abc/xyz/part is a actual physical file on disk try following (if actual file got some extension then append it).
Also add QSA flag so that query string is appended.
# 1. redirect uris which start with www. to the domain without www.
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L,QSA]
# 2. rewrite http://host/site/<name>/<uri> => http://<name>.host/<uri>
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{REQUEST_URI} ^/site/([^/]+)
RewriteRule ^(.*) http://%1.example.com/$1 [R=301,NC,L,QSA]
# 3. internal redirect to the corresponding directory
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ site/%1/ [L,NC,QSA]

Resources