htaccess and rewriting my URLs - .htaccess

I'm trying to organise my websites with .htaccess, and I want to following configuration:
The URL of the main site has to be beta.example.com/portal/
The other sites should be in beta.example.com/othersite/
If users go to beta.example.com, they should be redirected 301 to
beta.example.com/portal/
beta.example.com/portal (without the trailing slash) should redirect to beta.example.com/portal/
/portal/ is not a physical file path on the server. I want all these sites to
be in a folder together, say /html/www/websites/
The folder the portal resides in, should not be located in
/html/www/websites/portal/, but I want to be able to switch the live
site to a new version (in a different folder) on the server, quickly,
invisible, without any downtime. This goes for all sites by the way.
I'm currently working with two or three .htaccess files: one in the root (www.), which directs beta.example.com to the folder /websites/
#RewriteBase / #should I use rewritebase or not?
/RewriteCond %{HTTP_HOST} ^beta.example.com$
RewriteRule (.*) websites/$1 [L,NC]
A .htaccess file is in the folder /websites/, and maybe another one or maybe not in the folder a site resides in.
RewriteBase /websites # or, not...
# rewrite the active site to the folder /portal_v1.0/
RewriteCond %{REQUEST_URI} ^/portal/
RewriteRule (.*) portal_v1.0/$1 [L]
# rewrite the root to /portal/
RewriteRule ^$ http://beta.example.com/portal/ [L,NC]
However, I still can't get this to work exactly the way I want. One problem is what beta.example.com/portal redirects to www.example.com/websites/portal/ , I don't want that. Help! Thanks in advance!

Related

htaccess conditions to redirect variants of directory URL

I've dug around without much luck finding a solution.
I have a WordPress MultiSite using sub-directories as "temp" URLs so clients can review prior to mapping their domain name.
The login for the mapped domains is domain.com/ui which works with a redirect in my htaccess.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^ui$ /wp-login.php? [R=301,NE,NC,L]
I've also tried this RewriteRule ^ui(.*)$ /wp-login.php [L,R=301]
However, when attempting to login to a temp URL as described above it does not redirect.
I would like a solution that disregards sub-directories and for that matter any permalink structure and redirects /ui to /wp-login.php
Here are a few examples.
http://adopttheweb.com/ui
http://iemajen.com/emajenwebservices/ui
These are both on the same MultiSite blog. The first has been mapped to a domain whereas the second has not and is simply running at a "sub-directory" of the root / parent site.
Put the following code at main directory .htaccess file
RewriteEngine On
RewriteRule ^ui(.*)$ /wp-login.php [L,R=301]
You can also do it by PHP by putting header('Location: path/to/target.php'); at the ui directory index page etc

redirect /subdirectory to /symlink/subdirectory where /symlink points to /

i'm working on a drupal7 multisite setup based on subdirectories where example.com is used for the main website and example.com/subsite is another standalone drupal install. subsite is a symlink located in the root directory and also pointing to the root directory to give the subsite access to drupal core files.
now i have to make static content available via example.com/subsite/static, so i created a directory static in the root directory. that all works fine.
the problem is, that example.com/static is now also accessable and i want to prevent that.
i tried to redirect all requests to /static to /subsite/static resulting in inconsistent behaviour and redirect loops.
directory structure:
/
/{various drupal directories}
/subsite -> /
/static
rewrite rule:
RewriteRule ^/static/(.*)$ /subsite/static/$1 [R,L]
thx in advance
Is this what you want?
RewriteBase /
RewriteRule ^static/(.*)$ /subsite/static/$1 [L,R=301]
RewriteRule ^subsite/static/(.*)$ /static/$1 [L]
In the first line we redirect all call to /static/ to /subsite/static/.
In the second line we rewrite all call to /subsite/static/ to /static.
If it doesn't work please post your whole .htaccess file.
i fixed the redirect loops by using a rewrite condition, should have thought of that before.
RewriteCond %{REQUEST_URI} !^/subsite/(.*)$
RewriteRule ^static/(.*)$ /subsite/static/$1 [L,R=301]
thx anyway #florian-lemaitre for trying to help me

How can I redirect subdomain to folder while main domain points to another folder?

My very dear Stackoverflow community,
I have the following redirection problem and after several unsuccessful attempts I come here in search of enlightenment. My problem is the following. I have a domain, let's call it 'www.mydomain.com', and my 'public_html' directory has two folders as follows:
public_html
public_html/my_app/
public_html/my_other_app/
First, I would like that when typing the URL 'www.mydomain.com', I get redirected to the contents of folder 'my_app', while keeping the same URL. In fact this I have already accomplished, so whenever I type 'www.mydomain.com' I get redirected to 'www.mydomain.com/index.php', which actually corresponds to the 'public_html/myapp/index.php' script under 'myapp'.
Now I want to have a subdomain called 'other.mydomain.com', which has to redirect to contents of the 'my_other_app' folder, but I do not know how to make .htaccess work for this and at the same time work for the first case also.
So this is basically, the main domain redirects to one folder, and a subdomain redirects to another folder, and both folders are located under the public_html directory
Any hints more than welcome.
For your reference I post below my current .htaccess file:
RewriteEngine On
# redirect to www prefix
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# if start with www and no https then redirect
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# rewrite URL to trim folder
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^$ /login [L,R=301]
RewriteRule ^(.*)$ test/$1 [L]
This actually works for my main domain, it also rewrites the url to https. I need to add something in here in order to process separately the 'other.mydomain.com' and redirect to the '/my_other_app/' subfolder
what you need is a vhost (virtual host) per app. In the vhost, you will define the vhosts root directory, which will point to either of your sub directories.
There is IP based vhosts (one IP address per subdomain) or name based vhosts (the vhost is chosen based on the HTTP host header that all modern browser send).
But there is too much to say about vhosts to write it all here, just read the apache documentation here:
http://httpd.apache.org/docs/2.2/vhosts/
I think with pure .htaccess files, you can't do that (I might be wrong). Normally you would add vhosts in the main apache config. Based on your hosting, this may not be possible. Talk to you hosting provider in that case.
Marc

htaccess redirect remove /app/webroot in cakephp

I have an htaccess file that I want to redirect the all pages to the another folder. I am using cakephp for the site, except for this folder which I want to redirect.
"/app/webroot/" is added to the new directory so the url is /app/webroot/new/ instead of just new.
I am placing this htaccess file (below) in the "new" directory and want anything at /app/webroot/new/ to redirect to /new/ and remove the /app/webroot/ This folder is independent of cakephp and thus does not need to be processed by cake.
The code below loops and I am not sure why.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /new/
# If your concerned about direct access to a particular page without the sub-dir
# you will want to add something like this
RewriteCond %{REQUEST_URI} !^/new
RewriteRule (.*) /new/$1 [R=301,L,NC]
You should NOT place the .htaccess file in /new/ but in /app/webroot/new/.

Creating SubDomains to Absolute Paths with .htaccess

Hey, My host is absolutely terrible. For some odd reason creating a subdomain in cPanel simply does not work, and their support lines are always busy. I thought I could get around this by using .htaccess. I'm sure it's not that hard, but I'm kind of new to mod_rewrite and have had little success searching in the last 5 hours. Heres the situation:
/home/user/public_html automatically redirects to http://www.example.com
Since I'm using a CMS in public_html it has already added the rule in .htaccess to redirect anything unfamiliar after example.com/ to a 'Page Not Found'
/home/user/subdomain needs to redirect to http://subdomain.example.com
How should I go about creating a subdomain redirection to an absolute path? Or How can I add an exception in my .htaccess
I doubt you'll be able to get your subdomain to function outside of your public_html folder (although I'm no server admin). Typically that requires DNS modifications or tweaking the server's configuration. Have you tried making a sub-directory and rewriting calls to the subdomain? For example this placed in the .htaccess within your public_html directory:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteRule (.*) /subdomain/$1 [L]
I'm not sure if that would work (never needed to test it myself), but it's more likely to function than trying to target files that live outside the directory specified by the webhost as the location of your domain's files.
Good luck!
Try this rule:
RewriteCond %{REQUEST_URI} !^/home/user/
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ /home/user/%1%{REQUEST_URI} [L]
But your webserver already needs to be configured so that every request of foobar.example.com gets redirected to this specific virtual host.

Resources