1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

IF statements in htaccess?

Discussion in 'Apache' started by terryuk, Jun 22, 2007.

  1. #1
    I'm really stuck on this one, so I'd really appreciate it if someone could tell me if this is possible.

    I have;

    
    RewriteRule ^(.*)-(.*)-(.*).html$ index.php?go=$2&id=$3&resource=$1 [L,NC]
    
    Code (markup):
    The problem is thats an updated version of how it used it be so files are chaning and I need to be able to redirect the old ones to the homepage.

    So basically, if the first one (resource) is not filled in I need it to be redirected...?

    I can't find any logical way to do this using the PHP. I've tried doing if(!$resource) but it doesn't seem to care lol...

    Terry :(
     
    terryuk, Jun 22, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are lots of ways of creating conditional code for .htaccess, depending on what its for. For rewriting, you can use the RewriteCond directive - placed before a RewriteRule, the rule is only applied if the condition is met.

    I'm not really sure what you're asking - if you just want to add in another redirect for *-*.html to index.php, you could use:
    RewriteRule ^([^-]+)-([^-]+)\.html$ index.php [L,R]

    Or in PHP:
    if ( empty($_GET['resource']) ) {
        header('Location: http://yourdomain.com/index.php');
        exit;
    }
    PHP:
     
    rodney88, Jun 22, 2007 IP
  3. terryuk

    terryuk Notable Member

    Messages:
    3,962
    Likes Received:
    319
    Best Answers:
    0
    Trophy Points:
    255
    #3
    Neither of those work. I'll show you an example of what I need to do;

    Before I updated the details page on LinksJuice.com, detail pages looked like this;
    http://www.linksjuice.com/1/Arts_Entertainment/-detail-1857.html but now I have changed them so they use the title in the url so that one will now look like this; http://www.linksjuice.com/1/Arts_Entertainment/Celebrity_Pictures_Videos_Wallpapers-detail-1857.html.

    But now the issue is, that both of them are working.. and I need the old version without the link title in to get redirected to the homepage...

    I had tried doing the if empty statement, but that doesn't work so I had echoed $resource when the title is not being used in the URL and it shows '1/Arts_Entertainment/'.

    It's confusing me heh..
     
    terryuk, Jun 22, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I still don't quite understand what's going on - you're using a very loose regex pattern which captures a whole chunk of it (everything up to the first dash, so in this case 1/Arts_Entertainment/Page_Title) into the $1 backreference which must mean either the PHP script has to do further parsing to extract the ID or the information just isn't needed at all?

    But anyway, if it works for you there's no point changing it.. to do the redirect, you want to start matching at the last /, and redirect if a dash follows immediately.
    RewriteRule /-([^/-]*)-([^/-]*)\.html$ http://yoursite.com/index.php [R,L]
    And you would need to place that before the other rewriterule.

    You could even preserve the old links and 301 redirect to the corresponding new page - you just need to rewrite the request to send the ID to a lookup script that can grab the page title and generate the URL in the new format. Then the script could send a 301 moved header with the new location..
     
    rodney88, Jun 23, 2007 IP