redirect everything on the site except one folder and its subfolders - apache

I have a site, and I would like to forward everything expect one folder and it's subfolders
I need this for my .htaccess on Linux Apache
so for example everything under
www.mysite.com
www.mysite.com/random_folder
www.mysite.com/random_folder2
goes to extra domain
www.extradomain.com
but
www.mysite.com/except_me_folder
www.mysite.com/except_me_folder/subfolder
www.mysite.com/except_me_folder/?anyrequest
have no redirection
I tried this
RewriteCond %{REQUEST_URI} !^/except_me_folder/?.*$ [NC]
RewriteRule (.*) https://extradomain/$1 [R=301,L]

Related

htaccass subdomain redirecting problem with subfolder

In root .htaccess I have this command to redirect any subdomain to subfolder - for example john.anypage.com to anypage.com/subdom/john/
RewriteCond %{REQUEST_URI} !^subdom/
RewriteCond %{REQUEST_URI} !^/subdom/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
RewriteRule (.*) subdom/%2/$1 [DPI,L]
This works well, but I don't want to server leave direct access also to that subfolder from browser - https://www.anypage.com/subdom/john/
It create mess and doubles in google page search.
What I tried with rewrite in root or subdomain folder makes no action or unfinite cycle.
RewriteRule ^/subdom/john/(.*)$ https://john.anypage.com/ [R=301,L]
So I want to have permanent subdomain john.anypage.com redirect withnout possibility enter subfolder /subdom/john/ directly.

Apache mod_rewrite from a folder to a different domain doesn't work with trailing slash

I'm trying to rewrite URLs from old.domain.tld/project to domain.tld/subfolder/project using .htaccess in the project directory on the old server. I think I need to check for the host name so the rule only applies to the old server, even if the .htaccess file is also put on the new server.
I've tried the following .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteRule ^(.*)$ https://domain.tld/subfolder/project/$1 [R=301,L]
This works fine for URLs like https://old.domain.tld/project/index.php?p=q (redirects to https://domain.tld/subfolder/project/index.php?p=q) but not for the URL without the trailing slash—https://old.domain.tld/project ends up being redirected to https://domain.tld. Very odd! How do I make this work for both types of URL?
I tried a rather different method to make this a generic redirect, that could be included even if the folder name wasn't project, but this has the same problem:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule . https://domain.tld/subfolder/%1 [R=301,L]
In a server-wide configuration, a slash is appended to all requests for index files, e.g. old.domain.tld/project is redirected to old.domain.tld/project/. Could this be causing a problem?

301 redirect not working properly (my guess is b/c I'm dealing with Add-on domains)

I have a primary/account domain (PrimaryDomain.com) and a couple add-on domains (AddOnDomain1.com and AddOnDomain2.com) I've recently switched the content from AddOnDomain1.com to AddOnDomain2.com and I want everything to redirect to the exact same location (folders, files, images, etc.)
Here's what I have in .htaccess of my public_html folder (for PrimaryDomain.com):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.AddOnDomain1\.com$
RewriteRule (.*) http://AddOnDomain2.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^AddOnDomain1\.com$
RewriteRule (.*) http://AddOnDomain2.com/$1 [R=301,L]
It trys to redirect but here's what happens (with an image as an example). When I enter:
http://AddOnDomain1.com/img/logo.png
It redirects to:
http://AddOnDomain2.com/AddOnDomain1/img/logo.png
Notice how it adds that folder (in bold)?...which of course is the add-on domain directory name. Anyone aware of a fix for this scenario?

Redirecting shows wrong url

I'm working a long time on this issue but I still didn't found a solution.
I have on a hosted sever a number of domains. My primary domain is directed to my public_html directory. For my second domain if have created a folder site2.
So www.site1.com is directed to public_html and www.site2.com is redirected to dir site2. So far no problem. Both sites seems to work ok.
But if a navigate to www.site2.com/extra_content the url that is shown is www.site1.com/site2/extra_content. If I use a traling slash on my url, the correct URL (www.site2.com/extra_content) is shown. So I added the solution for the trailing slash. But it doesn't work.
My .htaccess looks like this:
#ADD TRAILING SLASH TO DIRECTORY IF NONE EXISTS
RewriteRule ^([^\.]+[^/])$ http://%{HTTP_HOST}/$1/ [L]
RewriteCond %{HTTP_HOST} ^(www)?site2.com$ [NC]
RewriteCond %{REQUEST_URI} !site2/ [NC]
RewriteRule ^(.*)$ /site2/$1 [NC]
Probably I missed something, so any help would be very much appreciated

Redirect all pages in direcory except one subdirectory .htaccess

I want to have one URL still being served from a subdirectory on the old domain but redirect all others from the parent directory to the new domain.
www.olddomain.co.uk/directorya/directoryb -> no redirect
www.olddomain.co.uk/directorya -> www.newdomain.co.uk/directorya
This works if the URL is typed with a trailing slash
RewriteCond %{REQUEST_URI} !^/directorya\/directoryb [NC]
RewriteRule ^(.*)$ http://www.newdomain.co.uk/ [L,R=301]
So http://www.olddomain.co.uk/directorya/directoryb/ works but http://www.olddomain.co.uk/directorya/directoryb fails.
Tried added $ at the end and other connotations and tried
RewriteEngine on
RewriteRule !^myspecialdirectory/mynextdirectory($|/) http://newdomain.example%{REQUEST_URI} [L,R=301]
from Redirect entire site except one directory to a new site - apache .htaccess
What do I need to do to get http://www.olddomain.co.uk/directorya/directoryb/ or http://www.olddomain.co.uk/directorya/directoryb to redirect? (while all other http://www.olddomain.co.uk pages go to http://www.newdomain.co.uk
For reasons I don't understand http://www.olddomain.co.uk/directorya/directoryb goes to Google and doesn't redirect
Try using THE_REQUEST variable and make sure this this very first rule:
RewriteCond %{THE_REQUEST} !\s+/+directorya/directoryb [NC]
RewriteRule ^ http://www.newdomain.co.uk/? [L,R=301]
Test it after clearing your browser cache.