htaccess specific folder redirecting to sub domain - regex

I've been having trouble using .htaccess to redirect any path starting with a specific directory to a sub domain. The main issue may be the directory actually exists as it is what my sub domain points to. I am not concerned about maintaining the path, redirecting to the home page for each is fine. I also need this for several specific domains ie:
website.com/foo -> foo.website.com
website.com/foo/about.html -> foo.website.com
website.com/foo/other/index.html -> foo.website.com
website.com/bar -> foo.website.com
website.com/foo/about.html -> foo.website.com
website.com/foo/other/index.html -> foo.website.com

It looks like you want to Redirect a specific folder to a specific host. As an alternative to using mod_rewrite you could use mod_alias and RedirectMatch.
RedirectMatch ^/foo[$/]? http://foo.website.com/
RedirectMatch ^/bar[$/]? http://foo.website.com/
You could of course add more rules to fit your setup.

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^foo\. [NC]
RewriteRule ^(foo|bar)(/.*)?$ http://foo.%{HTTP_HOST}/ [L,R=301,NC]

Related

Need the right .htaccess configuration

Let's say I have a domain called test.com.
Now my index.html is situated in /public_html/client/frontend/questionnaire/index.html, so I can access it in my browser via http://test.com/client/frontend/questionnaire/index.html.
What I want to achieve is:
* As soon as a user enters the url http://test.com, he or she should directly access the index.html
* Restrict the user so that he can only access this index.html file and not other directories on the server.
I think it should work somehow with the correct .htaccess configuration.
Any help would be great :)
You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^$ client/frontend/questionnaire/index.html [L]
Here Options -Indexes is used to disable directory listing.

Redirect paths with .htaccess from main domain to subdomain - multisite installation

I have my drupal setup at www.my-main-domain.com,
i have drupal multisite installation at subdomain.my-main-domain.com
I want to redirect all:
www.my-main-domain.com/user , www.my-main-domain.com/users,
www.my-main-domain.com/user/*
paths to my subdomain subdomain:
my-main-domain.com/user , subdomain.my-main-domain.com/users,
subdomain.my-main-domain.com/*
respectively.
I'm writing following in my .htaccess file:
RewriteBase /
RedirectMatch 301 ^/users/(.*)$ http://subdomain.my-main-domain.com/users/$1
RedirectMatch 301 ^/user/(.*)$ http://subdomain.my-main-domain.com/user/$1
RedirectMatch 301 ^/users$ http://subdomain.my-main-domain.com/users
RedirectMatch 301 ^/user$ http://subdomain.my-main-domain.com/user
But it redirects to subdomain but in continues loop and hence ends with blank display.
Please note that it's multisite installation with common root .htaccess file.
I have tried path redirect drupal module but its not supporting internal paths redirection saying paths already in use.
Please help. thanks
Use mod_rewrite instead to control the host name in rewrite condition.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^my-main-domain\.com$ [NC]
RewriteRule ^(users?)(/.*)?$ http://subdomain.my-main-domain.com/$1$2 [L,R=301,NC]
PS: Also note that your 4 rules can be combined into one as shown above.

Rewrite all requests through a directory

My site www.example.com has its document root to public_html directory. But, all of my content is hosted in public_html/www.example.com/. So, I want when a user visits my site he get contents from public_html/www.example.com/ and not public_html. So, what should be the .htaccess rewrite rule for that?
I am not good in rewrite rules but the best I found was:
RewriteRule !^blog blog%{REQUEST_URI}
There are two problems with this.
It will not work properly if user requested www.example.com/blog/
It will also not work when we use directory name as www.example.com
Give this a try and let me know if it works for you, and if it doesn't try to describe as detailed as possible what happens.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/www\.example\.com [NC]
RewriteRule ^(.*)$ /www.example.com/$1 [NC,L]

mod-rewrite to re-direct to https and a /secure/ folder?

I was wondering if there is anyway to do a mod-rewrite so if the user types http://www.url.co.uk, it will automatically re-direct them to https://www.url.co.uk/secure/folder/
? If not, I can move everything into the root, and then do a PHP re-direct if there's no https, but that's not ideal for this case...
Thanks!
Sure it can be done using mod_rewrite. Place this code in your .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^$ https://%{HTTP_HOST}/secure/folder/ [L,R]
That was answered here:
HTTP to HTTPS using mod_rewrite
You can easily add your custom folder in the statement on the third line.

Redirecting from a domain to a specific url of a Django project

I own two domains example1.com and example2.com. I setup example1.com to point to the the "/" of my Django project, meaning that example1.com/* is handled by urls.py. I want example2.com to actually point to a specific Django URL (e.g. "/123"), which is currently accessed at example1.com/123.
Ideally, when you go to example2.com/abc, Django would treat the request as if it came from example1.com/123/abc, but the URL would still be "example2.com/abc".
Here's what I have currently in my .htaccess file, but example2.com leads to "Server not found" in Firefox.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example2\.com$ [NC]
RewriteRule ^\.com(.*)$ http://example1.com/123/$1 [L]
This is my first experience doing something like with the htaccess file, so apologies if something is off. I am using mod_wsgi (on WebFaction)--does this still support rewriting URLs? Thank you for your help.
Try adding the following to your htaccess file in the root directory of your example2.com site.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example2\.com$ [NC]
#proxy all requests to example1.com/123
RewriteRule (.*) http://example1.com/123/$1 [P]
I needed to edit httpd.conf, as opposed to .htaccess. Here are the relevant lines:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)?example2.com$ [NC]
RewriteRule ^(.*)$ http://example1.com/123/$1 [P,L]
These lines go directly beneath the WSGIScriptAlias that directs to the Django project .wsgi file. I don't know why the .htaccess file did not do the trick, but hopefully this helps out others with the same problem.
Thanks!