make url.php redirect useing rewrite

Discussion in 'Apache' started by So1, Oct 24, 2008.

  1. #1
    i wanna make a redirect on my site.
    i've wrote a php-script like this:

    url.php
    
    if (@$_GET['url'])
    {
    	header("Location: " . $_GET['url']);
    }
    else
    {
    	die ("Couldnt read link");
    }
    
    Code (markup):
    well, if i use link like http://mydomain.zone/url.php=http://forums.digitalpoint.com/
    script will redirect me to this forum.
    but my link isnt pretty :)

    i want use
    http://mydomain.zone/url/http://forum.digitalpoint.com/
    its better...

    i write in my .htaccess the command to rewrite
    url.php?url=$1 to ^url/(.+)$
    Something like this:
    RewriteRule ^url/([a-zA-Z\:\/\.]+)$ url.php?url=$1 [L]
    or like this (more simple):
    RewriteRule ^url/(.+)$ url.php?url=$1 [L]
    I thought it will rewrite correctly, but it trims second slash after http:...
    Confused... why?
    if i use rule to make rewrite to the [R]ight and use link http://mydomain.zone/url/http://digitalpoint.com (enter to address in my browser) with rewriteRule:
    RewriteRule ^url/(.+)$ url.php?url=$1 [R]
    i'll get to http://mydomain.zone/url.php?url=http:/digitalpoint.com

    whats wrong and how to make it correct?

    Thanks in advance and sorry for my poor english ... )
     
    So1, Oct 24, 2008 IP
  2. mortenb

    mortenb Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/url/((f|ht)tps?://(.+))$
    RewriteRule . url.php?url=%1 [L]

    I've taken the liberty to add support for ftp and https urls too.
     
    mortenb, Oct 26, 2008 IP
  3. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This is a weird one. I've never seen a backreference drop a slash like that before.

    You could try a more generic regex like in mortenb's example. (.+) instead of ([a-zA-Z\:\/\.]+) however his example won't work in a .htaccess file unless you remove the leading slash on the regex. It will work fine in an Apache conf file as it is.

    I have a suspicion that it might be the escaping of characters that don't need to be escaped that could be the problem.
    Maybe this:
    RewriteRule ^url/([a-zA-Z:/\.]+)$ url.php?url=$1 [L]
    Code (markup):
    It could be the PHP that is the problem rather than the Apache regex. It also occurred to me that we don't need the PHP in here at all if that is all it does.
    Try this one:
    RewriteCond %{REQUEST_URI} ^url/(.+)$
    RewriteRule . %1 [R]
    Code (markup):
     
    Ladadadada, Oct 26, 2008 IP
  4. mortenb

    mortenb Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This is not true. It would have been true on a RewriteRule, but on a RewriteCond the leading slash is supposed to be there. Even in .htaccess.
     
    mortenb, Oct 27, 2008 IP
  5. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Really ?

    I haven't actually ever tried a RewriteCond in a .htaccess before. I just don't use them.

    It makes sense though. The RewriteCond is acting on the variable %{REQUEST_URI} which has a leading slash but the RewriteRule is acting on the part of the URI that is relative to the location of the .htaccess file which is usually after /. (...and will always be after at least one slash.)

    I've learned something today.
     
    Ladadadada, Oct 27, 2008 IP