【问题标题】:htaccess call php script then continue processinghtaccess 调用 php 脚本然后继续处理
【发布时间】:2017-12-14 05:27:34
【问题描述】:

我有这个特定的问题,我必须检查 URL,如果它的部分是保存在我的数据库中的 8 个字符长的哈希码,或者它只是你想要导航的普通 URL。

例如,如果我写网址:

- www.example.com/A4s8Gga3 
i want to process it with my script in php file

如果我写网址:

-www.example.com/my-books
-www.example.com/about
i want to navigate on those pages.

我知道我必须使用 htaccess(我自己管理了这么多),到目前为止它看起来像这样:

#part1
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (\/\w+)$ [NC]
RewriteRule ^(.*)$ mobile_redirect.php [L]

#part2
RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php

我的 mobile_redirect.php 如下所示:

ob_start();

require_once('connect.php');

$request = $_SERVER['REQUEST_URI'];
$request_hotovy = str_replace('/', '', $request);
$request_hotovy = mysql_real_escape_string($request_hotovy);

$select = "SELECT HASH_ID,OFFER FROM kasko_send_form WHERE MOBILE_HASH_ID = '".$request_hotovy."'";
$query = mysql_query($select);
if(mysql_num_rows($query) > 0){
    // request is mobile hash id
    $result = mysql_fetch_array($query);
    $hash_id = $result['HASH_ID'];
    header("Location: some_link?def=".$hash_id);
} else {
    // request is normal url
    header("Location: ".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}

我知道它最终会在循环中重定向。我试图将 part1 放在 part2 之后,但仍然有同样的问题。我正在使用 joomla,它有很多不是真正的目录或文件的 url(我无法写下来),这就是为什么我不能只在我的 php 文件中使用这个解决方案:

ob_start();

require_once('connect.php');

$request = $_SERVER['REQUEST_URI'];
$request_hotovy = str_replace('/', '', $request);
$request_hotovy = mysql_real_escape_string($request_hotovy);

$select = "SELECT HASH_ID,OFFER FROM kasko_send_form WHERE MOBILE_HASH_ID = '".$request_hotovy."'";
$query = mysql_query($select);
if(mysql_num_rows($query) > 0){
    // request is mobile hash id
    $result = mysql_fetch_array($query);
    $hash_id = $result['HASH_ID'];
    header("Location: some_link?def=".$hash_id);
} else {
    // request is normal url
    header("Location: page_not_found.php");
}

因为在 joomla 结束阅读我的 htaccess 之后,显然有更多的 url 处理完成(我对 joomla 也不太了解)。

你们能否给我一个提示如何处理 url(然后可能会改变它,使其不会进入循环,然后在 part1 恢复正常后再次改变它,以便它可以像往常一样继续处理)?​​

另外,如果你们有任何好的教程可以让我学习这些东西,那将非常有帮助,因为我只了解正则表达式的基础知识以及 htaccess 的工作原理......

【问题讨论】:

    标签: php apache .htaccess mod-rewrite joomla


    【解决方案1】:

    如果您将 Joomla 用于您的大多数 URL,则与应该包含这八个字符串的 URL 完全相同,那么有一个简单的解决方案。

    只需使用常规的 Joomla .htaccess 文件并在 RewriteBase / 之前添加两行

    RewriteCond %{REQUEST_URI} ^/[A-Za-z0-9]{8}$
    RewriteRule .* mobile_redirect.php [L]
    

    但这里的问题是,如果您在 Joomla 中有 8 个字符的常规 URL,那么它们将被重定向,例如http://example.com/lastnews

    所以对于这个 URL,你必须添加一个异常,并且这个洞会像这样锁定:

    RewriteCond %{REQUEST_URI} !^/latesnews$ [NC]
    RewriteCond %{REQUEST_URI} !^/youandme$ [NC]
    RewriteCond %{REQUEST_URI} ^/[A-Za-z0-9]{8}$
    RewriteRule .* mobile_redirectt.php [L]
    

    如果您的脚本在数据库中找不到记录,则无法使用相同的 URL 重定向回 Joomla。您的脚本正在处理 URL 或 Joomla 处理它。所以你必须提供一个 404,或者想办法在你的脚本中包含来自 Joomla 的 index.php 文件。

    【讨论】:

    • 因为这有点跑题了,所以我写了一个额外的命令:恕我直言,正确的方法是写一个 Joomla 插件。如果找不到数据库记录,则不必处理重定向。
    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    相关资源
    最近更新 更多