Apache 2.2 - "Split" domain - apache

I'm very new to Apache and already run into an problem which already takes a lot of time and I'm not even sure if it's possible.
I have two servers and one Domain called szop.in which is having an A record to my first server. On the first server I'm running an URL shortener called YOURLS, it's under szop.in/admin. I want the second server serve my homepage, therefor I want to redirect all requests like szop.in or http://subdomain.szop.in to the second server but not http://szop.in/admin.
Is this possible?
This doesn't seem to be the right solution and the mod_rewrite is causing me some headache:
RewriteEngine On
RewriteCond %{HTTP_HOST} szop.in [NC]
RewriteRule !^/admin$ hxxp://other-domain.in [R=301,L]
My idea was, since I need just one URL to work on the first server http://szop.in/admin, to redirect everything that is not starting with /admin to the other domain.

You almost got it:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^szop\.in$ [NC]
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteRule ^ http://subdomain.szop.in%{REQUEST_URI} [R=301,L]
You cannot use the negation on the RewriteRule like that, you use it on a conditions.
This should do what you want, it verify if domain is szop.in and if folder is not /admin and redirect to subdomain.szop.in.

Related

Redirection between domains

I have two webs on two different domains. One is on domain.com and the other web is on domain.es.
I am shutting down the web on domain.es but first I have to redirect every request to domain.es to domain.com. This is easy and I had already done it with RewriteCond & RewriteRule, like this:
www.domain.es/ -- redirects to --> www.domain.com/
But I am having a problem now. I also need to redirect one specific page from domain.es to domain.com like this:
www.domain.es/a_page/ -- redirects to --> www.domain.com/another_page/
I am having problems when redirecting with both rules, as when apache detects a request to www.domain.es/a_page it goes to www.domain.com/ and not to the specific page I need.
I have this in my virtualhost:
RewriteCond %{HTTP_HOST} ^domain.es/a_page/
RewriteRule ^(.*)& http://www.domain.com/another_page/ [R=301,L]
RewriteCond %{HTTP_HOST} ^domain.es/
RewriteRule ^(.*)& http://www.domain.com/ [R=301,L]
I've got the [L] flag so apache would stop processing rules, but this is not working.
Any help would be appreciated.
RewriteCond %{HTTP_HOST} ^domain.es/a_page/
The line above is never going to match anything - you're asking if the host is equal to domain.es/a_page/, which is a host as well as a path. You need to test against only the hostname, and then redirect based on the path, e.g.
RewriteCond %{HTTP_HOST} ^domain\.es
RewriteRule ^a_page/ http://www.domain.com/another_page/ [R=301,L]

TLD specific 301 rewrite for relaunch on new server

the company I work for is relaunching their newly built website on a new server under the same domain as before.
Example:
www.website.com/company/info/news.html on server A is going to change to www.website.com/news on server B.
On server A there is a second website (mostly a clone, so the same links are used) under a different TLD (website.ca). So there exists also a www.website.ca/company/info/news which should not be redirected.
We are relaunching only website.com for now so I need TLD-specific redirects. Everything combined there are about 250 pages that need redirecting to the new equivalent.
Those redirects are mainly aimed at crawlers and bots so we do not lose our pagerank and also for visitors with bookmarks, so I will add the rewrite-rules to the .htaccess-file on both servers.
I'm having trouble finding examples for this very specific scenario, so this is what I got so far from various how-tos and tutorials:
RewriteCond %{HTTP_HOST} ^http://www.website.com/company/info/news.html$ [NC]
RewriteRule ^(.*)$ http://www.website.com/news [L,R=301]
From what I understand it should redirect http://www.website.com/company/info/news.html to http://www.website.com/news with the information that it is a 301 and case-insensitive matching for the URL I want to redirect.
If this is correct I will add 250 RewriteCond (1 for each "old" page) and 1 RewriteRule per "new" page (about 10 in total) in this style:
RewriteCond 1
RewriteCond 2
RewriteRule 1
Thanks for any help in advance!
When checking for domain name, it is correct to look into %{HTTP_HOST}, but since it contains only the domain name (without the REQUEST_SCHEME or REQUEST_URI) you should compare it to www.website.com
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
For your exact example, the .htaccess RewriteRule would be:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^company/info/news.html$ /news [L,R=301]
But such exact rules would require you to write new Cond/Rule pait for every single .html file. If your structure follows some pattern, maybe something like this would work:
RewriteRule ^company/info/([a-zA-Z0-9_-]+).html$ /$1 [NC,L,R=301]
That should rewrite
http://www.website.com/company/info/anything.html to
http://www.website.com/anything

Apache redirect from one subdomain to another

Does anyone know a way to do a permanent redirect from a.example.com to b.example.com? I have other subdomains that need to remain as they are though. so:
first.example.com -> second.example.com
third.example.com is fine
fourth.example.com is fine
I want to do this in the <VirtualHost> block rather than an .htaccess to avoid having to redeploy again.
Any thoughts would be appreciated, thanks
Add the following RewriteRule to your VirtualHost
RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.example.com$
RewriteRule ^ http://second.example.com [R=301,L]
If you wanted to redirect first.example.com/some/url to second.example.com/some/url:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.example.com$
RewriteRule /(.*) http://second.example.com/$1 [R=301,L]
Recommend you use [R=302] whilst testing the rules to avoid problems with 301s getting cached by your browser.

Mod Rewrite, Unexpected Results

We are trying to redirect everything from one domain to another with the following
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example2.com%{REQUEST_URI} [R=301,L]
When we visit http://www.example.com/v2sc
We are being redirected to http://www.example2.comv2sc
We would like to be redirected to http://www.example2.com/v2sc considering www.example2.comv2sc is not a valid hostname
Any ideas on how we can accomplish this?
Thank you!
It seems like you're using a .htaccess file for this. In that context the leading slash is not present in %{REQUEST_URI} so it's up to you to put it back in.
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example2.com/%{REQUEST_URI} [R=301]
Please also note that solutions like this should be used only if you cannot edit the main server configuration file. Doing so would allow you to use a cleaner combination of vhosts and Redirect directives that would run much more quickly.

htaccess / mod_rewrite for Search Engine Friendly URLs

I have a bunch of files in the web root: -
/abc-674.php
/def-643.php
etc.etc.
I want to show these when the following URL's are requested, without changing the URL in the browser: -
/products/abc-674/this_can_be_anything.php
/products/abc-674/or_this.php
both redirect to /abc-674.php, and
/products/def-643/this_can_be_anything.php
/products/def-643/or_this.php
both redirect to /def-643.php.
So, basically, the bit between products/ and the next / is the target, while anything after that can effectively be ignored.
If it matters, I've already got a little code in my .htaccess to direct all traffic to my preffered domain (with www): -
# Direct all traffic to domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^domain$ [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [R=301,L]
EDIT: To clarify - the 2nd last line in the above is because I access the site on my dev server via http://domain/ only, and so don't want that rewrite to apply for those requests. I do, however, want this new rewrite to apply to all requests.
Any help greatly appreciated!
I think, you can use the next rule in .htaccess:
RewriteRule ^products/([\d\w]+)/(.*)$ /$1.php