【发布时间】: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