htaccess Rewrite path to subdomain except the Index Document - apache

I have the following structure in my website and I need to redirect all the traffic to a subdomain
Structure:
domain.com/subdomain/
.../folder1/folder/1.txt
.../folder1/folder/2.txt
.../folder2/folder/1.txt
.../folder2/folder/1.txt
.../index.php
I want to redirect all the traffic except the index.php or (domain.com/subdomain/) to a subdomain
Example:
domain.com/subdomain/folder1/folder/1.txt ---> subdomain.domain.com/folder1/folder/1.txt
domain.com/subdomain/folder1/folder/2.txt ---> subdomain.domain.com/folder1/folder/1.txt
But
domain.com/subdomain/index.php ---> No redirect
domain.com/subdomain/ ---> No redirect
This is what I came up with:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ http://subdomain.domain.com/$1 [NC]
This works for all the request but I want to exclude both / & index.php from this RewriteRule
Thanks!

You'll need a bit more than you have there. You must first check the domain with RewriteCond. The pattern ^subdomain/?(index\.php)?$ should match requests to the subdomain root or index.php with or without /. Technically it would also match the invalid subdomainindex.php without a / between but that would result in a 404 anyway.
RewriteEngine On
# If the requested domain isn't already the subdomain...
RewriteCond %{HTTP_HOST} !^subdomain\. [NC]
# Rewrite it to the subdomain
# unless it is a request for index.php or /
RewriteRule ^subdomain/?(index\.php)?$ - [L]
RewriteRule ^subdomain/(.*) http://subdomain.com/$1 [L,R=301,NC]

Related

How to Blanket Redirect all URL's to Different Domain Sub-directory no Mask

.htaccess Rules
Redirect 301 "/page-1/" "https://www.newdomain.com/page-1/"
Redirect 301 "/anotherpage/run/" "https://www.newdomain.com/anotherpage/run/"
Redirect 301 "/" "https://www.newdomain.com/subdirectory/"
Current Results
The first two rules which are specific pages work well. The problem is with the catch all which will carry on the URI at the destination url. Only needing for anything other than specified URI rules to 301 to the new domain sub directory.
eg.
https://olddomain.com/page-5/ to https://www.newdomain.com/subdirectory/
Expected Results
https://olddomain.com/page-1/ redirects to https://www.newdomain.com/page-1/
https://olddomain.com/anotherpage/run/ redirects to https://www.newdomain.com/anotherpage/run/
https://olddomain.com/anypage/fun/ redirects to https://www.newdomain.com/subdirectory/
I've tried a couple Rewrite rules with no luck. It carries the complete URI to the new domain.
RewriteRule ^(.*)$ https://www.newdomain.com/subdirectory/$1 [R=301,L]
Check this rules on the top of your olddomain.com .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
# custom rewrites
RewriteRule ^page-1/$ https://www.newdomain.com/page-1/ [R=301,L]
RewriteRule ^anotherpage/run/$ https://www.newdomain.com/anotherpage/run/ [R=301,L]
# rewrite all not custom matched requests to https://www.newdomain.com/subdirectory/
RewriteRule ^(.*)$ https://www.newdomain.com/subdirectory/ [R=301,L]
</IfModule>

redirectMatch in mod rewrite

We recently moved our web site to another server, say http://newdomain.com, while our old site is at say http://olddomain.com
I would like to keep the users directory on the old site accessible as it was before, that is http://oldomain.com/~user, but anything accessible under the root folder point to the new domain. The users directory on the old server are in the /home/user file system. So, I wrote following rules:
RedirectMatch permanent ^/~(.*) /home/~$1
RedirectMatch permanent ^/(.*) http://newdomain.com/$1
while the second rule works flawlessly, the first one still wants to map to the new site as following: http://newdomain.com/~user.
How can I fix the two rules so that anything in /var/www/html on the old site redirects to the new site, but anything under /home does not redirect?
--
Here is the new code based on Ben's solution, which maps the urls for the access to users' home pages correctly, but the browser complains and does not show their sites. The root folder urls redirection works fine.
RewriteEngine on
# RewriteBase / -- I had to comment this as the apache did not like it
Rewriterule ^/~(.*) /~$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/home/~
RewriteRule ^/(.*) http://newdomain.com/$1 [R=301,L]
You should use RewriteCond to test if the URI prefix with /home/~, and use RewriteRule to rewrite the URL and make a permanent redirect.
RewriteEngine On
RewriteBase /
RewriteRule ^~(.*) /home/~$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/home/~
RewriteRule ^(.*) http://newdomain.com/$1 [R=301,L]
Line 3: if URI prefix with ~{any_characters}, rewrite the URI to /home/~{any_characters}, redirect permanently
Line 5: Test if the URI not prefix with /home/~{any_characters}
Line 6: Then process the rewrite rule, rewrite the URI from {any_characters} to http://newdomain.com/{any_characters}, redirect permanently

htaccess redirection to one file for one domain and another file for another alias domain

I have 2 domains going to a vhost
example.com thisismyexample.com << this is an alias of the first one. It has got his own vhosts which is a symbolic link of the first one.
I need different behavior for each one so I'd like to redirect as:
if request comes for example.com -> example.com/index.php
if request comes for thisismyexample.com -> thisismyexample.com/admin.php
Both files index.php and admin.php are on the same directory which is my document root. At the moment there's a REQUEST_FILENAME rule and everything goes to index.php
I would try with redirectmatch but would like some opinions first
RedirectMatch ^example.com/$ http://example.com/index.php
RedirectMatch ^thisismyexample.com/$ http://thisismyexample.com/admin.php
Is this possible using htacces? If so, how would it be?
Use a RewriteRule with a RewriteCond that checks the host-header:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^ http://%{HTTP_HOST}/index.php [R,L]
RewriteCond %{HTTP_HOST} ^thisismyexample.com$
RewriteRule ^ http://%{HTTP_HOST}/admin.php [R,L]

htaccess rule to redirect root and rewrite all queries to another site

My domain is s1.example.com I want to rewrite all GET & POST to test.com/
I got the same done using the following rules.
RewriteEngine On
RewriteRule ^(.*) http://test.com/$1 [P]
But whenever there is a query for root, that is when user simply visits
http://s1.example.com/, the root of test.com shouldn't be served, rather the user should be redirected to issue a query, goTest.action
In other words, I want the DocumentRoot of s1.example.com to be a query, which is eventually handled by the rewrite rule above.
Just change your regex to .+ and have a new rule for root page:
RewriteEngine On
RewriteRule ^(.+)$ http://test.com/$1 [P]
RewriteRule ^/?$ /goTest.action [L]

htaccess redirect 301 (both www and non-www)

I have two domains:
http://www.domain.com
and
http://domain.com
each pointing to the same directory. Now I got a new domain (http://foo.com)
Unfortunately a catch all directive would not work, because names changed for subdirectories. I need about 20 single redirects, e.g.
/file1.html -> /newdir/
/x.html -> /y/
...
That means, both http://www.domain.com/dir1 and http://domain.com/dir1 should be redirected permanently to http://foo.com/newdir1. And the same with the other directories. How can I achive that via htaccess redirects?
Place this rule in root .htaccess of domain.com:
RewriteEngine on
ewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ http://foo.com%{REQUEST_URI} [L,R=301]
Try this
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^folder1(.*)$ http://www.newdomain.com/$1 [L,R=301]