3

Apache/PHP newbie question:

I'm trying to get an installation of http://yourls.org/ to work.

All seems to have installed OK, and I can add new urls and it shortens them.

However the links generated 404. E.g. http://mydoma.in/4 should redirect but doesn't

The .htaccess file looks like this:

# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourls-loader.php [L]
</IfModule>
# END YOURLS

and if I browse to the page http://mydoma.in/yourls-loader.php it does redirect me to the homepage.

I'm guessing there needs to be a parameter for the ID that needs passing via the rewrite to the /yourls-loader.php

The /yourls-loader.php looks like this:

<?php
// Handle inexistent root favicon requests and exit
if ( '/favicon.ico' == $_SERVER['REQUEST_URI'] ) {
    header( 'Content-Type: image/gif' );
    echo base64_decode( "R0lGODlhEAAQAJECAAAAzFZWzP///wAAACH5BAEAAAIALAAAAAAQABAAAAIplI+py+0PUQAgSGoNQFt0LWTVOE6GuX1H6onTVHaW2tEHnJ1YxPc+UwAAOw==" );
    exit;
}

// Handle inexistent root robots.txt requests and exit
if ( '/robots.txt' == $_SERVER['REQUEST_URI'] ) {
    header( 'Content-Type: text/plain; charset=utf-8' );
    echo "User-agent: *\n";
    echo "Disallow:\n";
    exit;
}

// Start YOURLS
require_once( dirname( __FILE__ ) . '/includes/load-yourls.php' );

// Get request in YOURLS base (eg in 'http://site.dom/yourls/abcd' get 'abdc')
$request = yourls_get_request();

// Make valid regexp pattern from authorized charset in keywords
$pattern = yourls_make_regexp_pattern( yourls_get_shorturl_charset() );

// Now load required template and exit

yourls_do_action( 'pre_load_template', $request );

// At this point, $request is not sanitized. Sanitize in loaded template.

// Redirection:
if( preg_match( "@^([$pattern]+)/?$@", $request, $matches ) ) {
    $keyword = isset( $matches[1] ) ? $matches[1] : '';
    $keyword = yourls_sanitize_keyword( $keyword );
    yourls_do_action( 'load_template_go', $keyword );
    require_once( YOURLS_ABSPATH.'/yourls-go.php' );
    exit;
}

// Stats:
if( preg_match( "@^([$pattern]+)\+(all)?/?$@", $request, $matches ) ) {
    $keyword = isset( $matches[1] ) ? $matches[1] : '';
    $keyword = yourls_sanitize_keyword( $keyword );
    $aggregate = isset( $matches[2] ) ? (bool)$matches[2] && yourls_allow_duplicate_longurls() : false;
    yourls_do_action( 'load_template_infos', $keyword );
    require_once( YOURLS_ABSPATH.'/yourls-infos.php' );
    exit;
}

// Prefix-n-Shorten sends to bookmarklet (doesn't work on Windows)
if( preg_match( "@^[a-zA-Z]+://.+@", $request, $matches ) ) {
    $url = yourls_sanitize_url( $matches[0] );
    if( $parse = yourls_get_protocol_slashes_and_rest( $url, array( 'up', 'us', 'ur' ) ) ) {
        yourls_do_action( 'load_template_redirect_admin', $url );
        $parse = array_map( 'rawurlencode', $parse );
        // Redirect to /admin/index.php?up=<url protocol>&us=<url slashes>&ur=<url rest>
        yourls_redirect( yourls_add_query_arg( $parse , yourls_admin_url( 'index.php' ) ), 302 );
        exit;
    }
}

// Past this point this is a request the loader could not understand
yourls_do_action( 'loader_failed', $request );
yourls_redirect( YOURLS_SITE, 302 );
exit;

The yourls_get_request() function looks like this:

function yourls_get_request() {
    // Allow plugins to short-circuit the whole function
    $pre = yourls_apply_filter( 'shunt_get_request', false );
    if ( false !== $pre )
        return $pre;

    static $request = null;

    yourls_do_action( 'pre_get_request', $request );

    if( $request !== null )
        return $request;

    // Ignore protocol & www. prefix
    $root = str_replace( array( 'https://', 'http://', 'https://www.', 'http://www.' ), '', YOURLS_SITE );
    // Case insensitive comparison of the YOURLS root to match both http://Sho.rt/blah and http://sho.rt/blah
    $request = preg_replace( "!$root/!i", '', $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 1 );

    // Unless request looks like a full URL (ie request is a simple keyword) strip query string
    if( !preg_match( "@^[a-zA-Z]+://.+@", $request ) ) {
        $request = current( explode( '?', $request ) );
    }

    return yourls_apply_filter( 'get_request', $request );
}
zessx
  • 68,042
  • 28
  • 135
  • 158
bendecko
  • 2,643
  • 1
  • 23
  • 33
  • can you check and see that apache's mod_rewrite module is enabled? – tchow002 Mar 13 '14 at 07:08
  • @TanvirChowdhury I uploaded a file info.php with – bendecko Mar 13 '14 at 07:33
  • is yourls installed in a subdirectory – tchow002 Mar 13 '14 at 07:43
  • @TanvirChowdhury no /var/www - have a look at my question to ravi below. I want to run a test to see ?whatparam=works then I can concentrate on the rewrite. can you look? – bendecko Mar 13 '14 at 07:48
  • I would guess you could change the value of $request after the line $request = yourls_get_request(); in yourls-loader.php – tchow002 Mar 13 '14 at 07:54
  • so http://mydoma.in/yourls-loader.php?request=1 should redirect me to that page ID in the db? Well it just redirects me to home page. – bendecko Mar 13 '14 at 08:01
  • I was telling you to modify yourls-loader.php. To add a line that says something like $request = 'abcd'; after the line $request = yourls_get_request();. If you are interested in how yourls normally gets its parameters I would check the yourls_get_request() function. I don't know if it is setup to take in a get argument like you are trying to do. – tchow002 Mar 13 '14 at 08:04
  • I've added the function, I will add the request line now. – bendecko Mar 13 '14 at 08:13
  • @TanvirChowdhury OK added the line $request = '1'; and it does not redirect, that does not seem to effect the call as the log I was directed to view below. I called http://2the.re/somethingelse with the above line in place, and in the log it says ubuntu@ip-10-0-0-243:/var/log/apache2$ File does not exist: /var/www/somethingelse – bendecko Mar 13 '14 at 08:21
  • The thing about that is - if your .htaccess file is not doing its job then you are never getting to your yourls-loader.php. So you were literally looking for that file and it was not there. Go to the yourls-loader.php directly and it should act as if you went to /1 since you have the request hardcoded. – tchow002 Mar 13 '14 at 08:23
  • 2the.re/yourls-loader.php takes me to home page, not to site assigned in ID1 – bendecko Mar 13 '14 at 08:30
  • It was this answer http://askubuntu.com/questions/48362/how-to-enable-mod-rewrite-in-apache. Since I wanted to use Mod Rewrite in .htaccess you have to edit the httpd.conf (which is moved to somewhere else) read http://askubuntu.com/questions/48362/how-to-enable-mod-rewrite-in-apache for the details. Thanks for your help anyway! – bendecko Mar 13 '14 at 08:40

2 Answers2

5

In order to use mod_rewrite you can type the following command in the terminal: (Assuming you are on debian based server)

$sudo a2enmod rewrite

Restart apache2 after

$sudo /etc/init.d/apache2 restart

or

$sudo service apache2 restart

Then use your .htaccess

# BEGIN YOURLS
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /yourls-loader.php [L]
</IfModule>
# END YOURLS
Ravi Dhoriya ツ
  • 4,435
  • 8
  • 37
  • 48
  • It is still 404. I want to test the redirection from the file is working. I can't make head nor tail from the php script. What is the /yourls-loader.php?thispara=xx What is the thisparam, so i can test by manually putting thisparam=1 into it. – bendecko Mar 13 '14 at 07:35
  • ubuntu@ip-10-0-0-243:/var/log/apache2$ [Thu Mar 13 08:06:21 2014] [error] [client 91.187.69.185] File does not exist: /var/www/4, referer: http://www.2the.re/admin/index.php It is looking for the file 4 (which is the redirect id) – bendecko Mar 13 '14 at 08:07
  • Can you check your apache httpd.conf file. Try changing AllowOverride None to AllowOverride All and restart apache and see if it works. – tchow002 Mar 13 '14 at 08:17
  • Thank you so much. Bit fiddly all this, and the fact that each distro has files in different places, and different configs. http://askubuntu.com/questions/48362/how-to-enable-mod-rewrite-in-apache as my httpd.conf file was empty https://help.ubuntu.com/12.04/serverguide/httpd.html tells me 'httpd.conf: historically the main Apache2 configuration file, named after the httpd daemon. Now the file is typically empty, as most configuration options have been moved to the below referenced directories. The file can be used for user specific configuration options that globally effect Apache2.' – bendecko Mar 13 '14 at 08:38
1

Page Not Found Error on Yourls Short url

Edit /etc/apache2/apache2.conf

sudo nano /etc/apache2/apache2.conf

search

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

and change it to;

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

then,

sudo service apache2 restart

"°º©o.,I solved the problem so wellL,.o©º°"