【问题标题】:.htaccess URL Rewrite Failing for Yourls - The requested URL was not found on this server您的 .htaccess URL 重写失败 - 在此服务器上找不到请求的 URL
【发布时间】:2014-04-17 18:19:17
【问题描述】:

Apache/PHP 新手问题:

我正在尝试安装 http://yourls.org/

一切似乎都安装好了,我可以添加新的 url 并缩短它们。

但是链接生成了 404。 http://mydoma.in/4 应该重定向但没有

.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

如果我浏览到页面http://mydoma.in/yourls-loader.php,它会将我重定向到主页。

我猜需要有一个 ID 参数需要通过重写传递给/yourls-loader.php

/yourls-loader.php 看起来像这样:

<?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;

yourls_get_request() 函数如下所示:

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 );
}

【问题讨论】:

  • 你能检查一下 apache 的 mod_rewrite 模块是否已启用?
  • @TanvirChowdhury 我用
  • 是你的安装在子目录中
  • @TanvirChowdhury no /var/www - 看看我对 ravi 的问题。我想运行测试以查看 ?whatparam=works 然后我可以专注于重写。你能看看吗?
  • 我猜你可以在 $request = yourls_get_request(); 之后更改 $request 的值。在 yourls-loader.php

标签: php apache .htaccess yourls


【解决方案1】:

为了使用 mod_rewrite,您可以在终端中输入以下命令:(假设您在基于 debian 的服务器上)

$sudo a2enmod rewrite

之后重启apache2

$sudo /etc/init.d/apache2 restart

$sudo service apache2 restart

然后使用您的.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

【讨论】:

  • 仍然是 404。我想测试来自文件的重定向是否有效。我不能从 php 脚本中得到头部或尾部。 /yourls-loader.php 是什么?thispara=xx thisparam 是什么,所以我可以手动将 thisparam=1 放入其中进行测试。
  • ubuntu@ip-10-0-0-243:/var/log/apache2$ [Thu Mar 13 08:06:21 2014] [error] [client 91.187.69.185] 文件没有存在:/var/www/4,引用者:2the.re/admin/index.php 它正在寻找文件 4(即重定向 id)
  • 你能检查你的 apache httpd.conf 文件吗?尝试将 AllowOverride None 更改为 AllowOverride All 并重新启动 apache 并查看它是否有效。
  • 非常感谢。这一切有点繁琐,每个发行版在不同的地方都有文件,以及不同的配置。 askubuntu.com/questions/48362/… 因为我的 httpd.conf 文件是空的 help.ubuntu.com/12.04/serverguide/httpd.html 告诉我'httpd.conf:历史上主要的 Apache2 配置文件,以 httpd 守护进程命名。现在该文件通常为空,因为大多数配置选项已移至以下引用的目录。该文件可用于全局影响 Apache2 的用户特定配置选项。'
【解决方案2】:

在 Yourls Short url 上找不到页面错误

编辑 /etc/apache2/apache2.conf

sudo nano /etc/apache2/apache2.conf

搜索

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

并将其更改为;

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

那么,

sudo service apache2 重启

"°º©o.,我很好地解决了这个问题L,.o©º°"

【讨论】:

    猜你喜欢
    • 2014-04-16
    • 2017-09-09
    • 1970-01-01
    • 2017-03-21
    • 2010-10-16
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2017-12-26
    相关资源
    最近更新 更多