dqm83011 2014-03-13 07:05
浏览 510
已采纳

.htaccess URL为Yourls重写失败 - 在此服务器上找不到请求的URL

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: *
";
    echo "Disallow:
";
    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 );
}
  • 写回答

2条回答 默认 最新

  • drblhw5731 2014-03-13 07:16
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用