htaccess redirect on a domain that already redirects - apache

We have 3 domains in play here. Domain1.com is the main site, domain2.com is the smaller second site, and domain3.com is a vanity URL that redirects from domain1 to a Spanish version of domain2.
What we need to have happen is for domain3.com/subpage to redirect to domain2.com/subpage, however the original redirect of the vanity URL is overriding any attempts we make.
Is this kind of redirect possible?
Here is the original redirect in domain1.com's htaccess file:
RewriteCond %{HTTP:Host} ^www\.domain3\.com$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version [I,R]
These are the attempts at making the needed redirect happen (also in domain1.com's htaccess file):
RewriteCond %{HTTP:Host} ^www\.domain3\.com/subpage$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version/subpage [I,R]
RewriteRule ^domain3\.com/subpage?$ /spanish-version/subpage [NC,R=302,U]

Rules for your domain3 should look like this :
RewriteCond %{HTTP:HOST} ^(www\.)?domain3\.com$ [NC]
RewriteRule ^/?(subpage_name)/?$ http://www.domain2.com/$1 [NC,R,L]
This will redirect
www.domain3.com/subpage
to
www.domain2.com/subpage
(www.)? in HTTP:HOST variable is to make the "www" part optional in url, so in this case
urls with or without "www" would also work.

Related

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 in subdirectory redirects to main domain

I have the following rules for url management which redirect non-www traffic to www and then rewrite some urls. It seems to work fine except when i go to "http://example.com/prosite/" (without www, the first redirection case - "^$"). I would expect this to bring the home page (meaning it should redirect to http://www.example.com/prosite/index.php?page=home) but instead it redirects me to "http://www.example.com". Thanks for your help.
RewriteEngine On
RewriteBase "/prosite/"
# redirect non-www addresses to www
# redirect condition: hostname does NOT contain 2 or more dots
RewriteCond "%{HTTP_HOST}" "!\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])\."
RewriteRule "^(.*)$" "http://www.%{HTTP_HOST}/prosite/$1" [R=301,L]
# redirect empty path to home page
RewriteRule "^$" "index.php?page=home" [NC,L]
# redirect page path to corresponding page
RewriteRule "^(home)/?$" "index.php?page=$1" [NC,L]
# redirect all other paths to index.php (without a "page" argument this will show the default page: 404 not found)
RewriteRule "!^index\.php$" "index.php" [NC,L]
Apparently it was a browser cache problem. I emptied the cache and it worked.

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.

.htaccess redirect folder to its subdomain causes redirect loop

I have a subdomain sub.domain.com
And the same contents are available on domain.com/sub
But now I want to redirect domain.com/sub to sub.domain.com
If I do that with
Redirect 301 /sub http://sub.domain.com
it causes a redirect loop.
How can this be solved technically? Users should not be able to access the subdomain content under domain.com/sub but should be redirected.
Better to use mod_rewrite rules here with conditions to restrict the rule only for domain.com requests:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteRule ^sub(/.*)?$ http://sub.domain.com/$1 [L,NC,R=302]
Make sure to test this after clearing browser cache.

Redirect traffic to CDN page

So anytime someone visits this page:
http://www.site.com/page/image_(.*).png
They get redirected to the cdn url, which would be
http://cdn.site.com/page/image_(.*).png
How can I do that using htaccess? I only need this for that url.
Using mod_alias, if you have access to vhost config, in your main vhost (for www) add:
RedirectMatch 301 ^/page/image_(.*)\.png http://cdn.site.com/page/image_$1.png
If you don't have access and have to use an .htaccess file, you need mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?site.com [NC]
RewriteRule ^page/image_(.*).png http://cdn.site.com/page/image_$1.png [R=301,L]