.htaccess rewrite across domain - apache

I have 3 pages:
http://www.domain.com/page-one
http://www.domain.com/page-two
http://www.domain.com/page-three
I would like to have a new domain (e.g http://domain2.com) and map these pages to the new domain using .htaccess and Apache rewrite rules.
so the following would be mapped:
http://domain2.com would show http://www.domain.com/page-one
http://domain2.com/info would show http://www.domain.com/page-two
http://domain2.com/contact would show http://www.domain.com/page-three
Thanks

You can't truly do a rewrite across domains, but you can do a redirect:
RewriteRule ^info$ http://domain.com/page-two [R=301]
(It might be possible to simulate a rewrite with some cURL trickery, if that's really what you're going for)

If both sites live in the same directory you could do something like this.
RewriteCond %{HTTP_HOST} ^domain2.com$ [NC]
RewriteRule ^$ /page-one
RewriteCond %{HTTP_HOST} ^domain2.com$ [NC]
RewriteRule ^info$ /page-two
RewriteCond %{HTTP_HOST} ^domain2.com$ [NC]
RewriteRule ^contact$ /page-three

Related

htaccess Rewrite URL based on TLD

On my website, I am interested in moving my forums from a subdirectory to a subdomain. The forums are currently located at example.com/forums, and I want to move them to forums.example.com. After the move, I want to use htaccess to 301 redirect all forum traffic to the subdomain, but the problem is that I have two TLDs for my site, a .com domain and a .net domain.
I am currently trying to redirect traffic using this:
RewriteCond %{HTTP_HOST} !=forums.example.net
RewriteRule ^forums(/(.*))?$ https://forums.example.net/$2 [L,R=301]
RewriteCond %{HTTP_HOST} !=forums.example.com
RewriteRule ^forums(/(.*))?$ https://forums.example.com/$2 [L,R=301]
This only half-works. Regardless of which TLD I visit, it always redirects me to forums.example.net, even if I am visiting from example.com/forums, in which case I want it to go to forums.example.com. How could I achieve this?
You can use this singe rule as very first rule in forums/.htaccess instead of top level .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.(?:com|net))$ [NC]
RewriteRule ^.*$ http://forums.%1/$0 [L,R=301,NE]
For root .htaccess use this rule as your first rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.(?:com|net))$ [NC]
RewriteRule ^forums(/.*)?$ http://forums.%1$1 [NC,L,R=301,NE]
Please use below rules for the proper redirection to work as you want it.
Rewritengine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^forums$ http://forums.example.com/? [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.net$
RewriteRule ^forums$ http://forums.example.net/? [L,R=301]

Redirecting a domain with htaccess with a few specific 301s

Is it possible to do something like this to redirect old traffic to the new site with a 301.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ http://new-example.com/$1 [L,R=301]
However, I will need to manually map some pages to their new equivalent pages like
Redirect 301 /about http://new-example.com/about-us
Is it possible to do both?
If you are going to use Rewrite, then use it only. I don't recommend use both mod-alias and mod-rewrite. You can use rewrite to map to individual pages too. Also the order matters. The catchall rule should be the last one. This would provide cleaner code IMO.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^new-example.com$ [NC]
RewriteRule ^about/?$ http://new-example.com/about-us [R=301,L]
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ http://new-example.com/$1 [L,R=301]

Apache wildcard subdomains with url masking

I am trying to handle wildcard subdomains with url masking in apache.
Correct rewrite rules should achieve the following:
http://demo.system.dev to to http://system.dev?subdomain=demo
http://sample.system.dev/user/edit/100 to to http://system.dev/user/edit/100?subdomain=sample
http://debug.system.dev/project/edit/new to to http://system.dev/project/edit/new?subdomain=debug
So far i have the following rule in my .htaccess
RewriteCond %{HTTP_HOST} ^(.*)\.system\.dev
RewriteRule ^(.*?)$ http://system.dev%{REQUEST_URI}?subdomain=%1 [L]
which looks like its working ok except that the browser url is also changed. I would like the browser url to remain the same and internally route the request but i am not sure how to achieve this.
Try this rule in your root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !(?:^|&)subdomain= [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.system\.dev$ [NC]
RewriteRule (.*) $0?subdomain=%1 [L,QSA]
You can not internally rewrite to another domain. So when you go to your subdomain it will redirect to main domain as you have it. So you will need to use relative URL and see if that works for you.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www\.)?system\.dev [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.system\.dev [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}?subdomain=%1 [L]

.htaccess change domain but keep path

i don't even know whether this is possible or not.
i've 3 domain names:
mytest.com
test88.com
test99.com
mytest.com is the main domain where all the content is located. in my case it is wordpress which is installed on that webspace.
my htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)test88.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test88 [R,L]
RewriteCond %{HTTP_HOST} ^(.*)test99.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test99 [R,L]
i want to keep the domainname in case a visitor goes to test88.com but i also want to keep the rest of the path. It should look like this in the address-bar:
http://www.test.88.com/wp/?page_id=10&test=test88
ist this possible?
thanks in advance
Do you want the content from mytest.com show up under the test88.com and test99.com, basically creating duplicates ?
In that case you probably don't want to mod_rewrite to redirect [R], but to reverse-proxy [PT] the content from the main domain.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)test88.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test88 [PT,L]
RewriteCond %{HTTP_HOST} ^(.*)test99.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test99 [PT,L]
Make sure mod_proxy is installed on your Apache.

Redirecting old domian to new domain by .htaccess

I am in process of migrating my old domain to new domain using Apache Mod-Rewrite moduled and its .htaccess file.
we have almost same structure of the new domain which includes
URL's
Database
except the domain name, like it was www.oldurl.com and now its like www.newurl.com and this is what i have in my .htaccess file of Old domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Above settings seems to be working fine except in one case, we have few URL's in my old domain which has either been removed or structure has been changed so in that case above rule will not work.i came to know about adding something like this in my .htaccess file beside what i have described above
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Redirect 301 /my-page http://www.newurl.com/your-page
i have total of 20+ such URL's and i am wondering do i need to map those all 20+ URL to there new URL's and will i need to take care about any order in which they should be put in the file.
i am also wondering how Apache will work, will it look at each mapped URL for any match? or it works in some other way?
The Redirect directive won't be bound to the RewriteCond conditions and will always redirect /my-page to http://www.newurl.com/your-page, also, mod_rewrite has precedence over mod_alias so the RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L] rule gets applied before the Redirect directive gets looked at. However, if the .htaccess file sits within the document root of both the oldurl.com and newurl.com domains, the Redirect directive will be applied after the browser gets redirected to http://www.newurl.com/my-page, thus redirecting (again) to http://www.newurl.com/your-page
So, it doesn't matter what order you have these in, since mod_rewrite gets applied first. If you have 20 URLs that need to redirect to new ones on your new site, you can enumerate them each in their own Redirect. Otherwise, if you'd rather not have the browser get redirected twice, you can enumerate them using the mod_rewrite engine:
RewriteEngine On
# redirect the changed URLs individually
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page$ http://www.newurl.com/your-page [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page2$ http://www.newurl.com/your-page2 [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page3$ http://www.newurl.com/your-page3 [R=301,L]
# Finally, redirect everything else as-is
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Note that order does matter here. Having to repeat the 2 Conditions for HTTP_HOST is kind of ugly, you can maybe get around that by using the SKIP, but it's probably better to just repeat them. But if you have access to your server config or vhost config, take a look at the RewriteMap Directive, which allows you to create a mapping of, in your case, old urls to new urls and you can reduce all the individual changed url rewrites to a single one:
Inside your server/vhost config, something like this:
RewriteMap newurls txt:/path/to/file/map.txt
Where the /path/to/file/map.txt will look something like:
my-page your-page
my-page2 your-page2
my-page3 your-page3
etc...
And your combined rules would look like:
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^(.*)$ http://www.newurl.com/${newurls:$1} [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]