Small things often cause pain – So why can’t I create a variable inside a switch statement?

Sometimes the smallest things can cause you pain. I had a nice example of this today. I’m still new to Objective C and find something new everyday. Was using a switch statement and had what at the time seemed like an odd little issue. I couldn’t seem to create variables and call methods. The error message I was getting back really wasn’t that helpful. Turns out it was as simple as surrounding the case statement calls in curly brackets. See below:

Fails:

case 2: 
    Alert *alert = [allAlerts objectAtIndex:indexPath.row];

    alertCell.textLabel.text = alert.date;//@"Date";
    alertCell.detailTextLabel.text = @"Alert Message";
    return alertCell;
 break;

Works:

case 2: {
    Alert *alert = [allAlerts objectAtIndex:indexPath.row];

    alertCell.textLabel.text = alert.date;//@"Date";
    alertCell.detailTextLabel.text = @"Alert Message";
    return alertCell;
} break;
http://stackoverflow.com/questions/4902568/how-do-i-create-a-variable-inside-a-switch-statement-for-a-uitableview

Yup all I need was those two curly brackets 😉 Now if only Xcode could have told me that!  Yeah I know this isn’t a big thing, just think it’s funny how the small things can be such a pain at times and how knowing the syntax of a language really can be an advantage at times. I guess the moral of the story is that small things really do matter.

 

More on redirecting with Mod_Rewrite

So it’s all well and good to redirect from one domain to another but how about redirecting to a directory?

I use shared hosting and host 3-4 domains on the same hosting account. To be nice and tidy I like to keep each site in a separate sub folder. There are a few ways I could do this obviously. I could use PHP and do something like the following:

$SiteNameURL = $_SERVER['HTTP_HOST'];
switch (strtolower($SiteNameURL)) {
case "domain.com": //MUST BE LOWER CASE
            header("Location: http://" . $_SERVER['HTTP_HOST'] . "/folder/");
            break;
case "www.domain.com": //MUST BE LOWER CASE
            header("Location: http://" . $_SERVER['HTTP_HOST'] . "/folder/");
            break;
}

But that is rather ugly and it would be nice to keep all the redirecting in one place.
This is what I came up with:

#Turns the rewrite engine on.
RewriteEngine on

#Fix missing trailing slash character on folders.
RewriteRule ^([^.?]+[^.?/])$ $1/ [R,L]

#www.domain.com and domain.com will map to the folder {root}/folder1/
RewriteCond %{HTTP:Host} ^(?:www\.)?domain\.com$
RewriteCond %{REQUEST_URI} !^/folder1/
RewriteRule ^(.*) folder1/$1 [NC,L,NS]

There is a little bit of regex involved but if you step through line by line it’s really fairly simple. Of course the last 4 lines are what matters for me. I added them to my “.htaccess” file from my first post on Mod_rewrite.

Redirecting from one domain to another domain with Mod_Rewrite

Lets get started with some of the things I’m doing while I setup this site and a few others I’m working on. First thing I needed to do was redirect a few domains elsewhere as this shared web hosting is used for some domains I look after for someone else. I can’t be bothered getting the DNS changed so to so I’m going to use mod_rewrite to redirect to the domain that is in use.

So here is what I found.

So if you need to change an existing website to another domain name your web server is using Apache HTTP Server then you can use the mod_rewrite module to do the task. I used something along the lines of the following code:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^OldDomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.OldDomain.com$
RewriteRule ^(.*)$ http://www.NewDomain.com/$1 [R=301,L]

Add this to the existing “.htaccess” file or add a new“.htaccess” file in your OldDomain‘s web root folder(example: /public_html/).

Done! Now all visitors to OldDomain.com(regardless the path) will auto redirected to NewDomain.com(with appending path).

For those of you who don’t want to fiddle with the “.htaccess” file you could just use PHP as follows:

$SiteNameURL = $_SERVER['HTTP_HOST'];

switch (strtolower($SiteNameURL)) {

case "domain.com": //MUST BE LOWER CASE
            include 'page1.php';
            break;
case "www.domain.com": //MUST BE LOWER CASE
            include 'page1.php';
            break;
case "yourotherdomain.com": //MUST BE LOWER CASE
            include 'page2.php';
            break;
case "www.yourotherdomain.com": //MUST BE LOWER CASE
            include 'page2.php';
            break;
default:
            include 'other.php';
}