Dus*_*der 17 php wordpress .htaccess mod-rewrite multisite
我正在为自定义WordPress多站点网络构建一个WordPress插件,并且有一些文件使用URL变量从第二个数据库(而不是WordPress数据库)加载信息.
在WordPress外部构建的版本中,通过导航到http://website.com/NAME-HERE它将检查它是否是我的数据库中的用户名,如果不检查它是否在我的数据库中是一个组并加载与其用户名或组相对应的文件.
现在我完全迷失了将其实现到WordPress Multisite中.据我所知,插件无法.htaccess重写?如何使其在所有网站上运行?
如果不是最好的方法来完成这个?
我是否需要将我的pretty.php&.htaccess重写放入我的根目录并将页面指向插件中的文件?如果是这样,它如何与多个主题一起使用?
我唯一的另一个想法是,这可以用类似的东西来实现RewriteMap.
在这一点上,我认为我最好的选择是联系StackOverflow社区寻求帮助或指导.
的.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w.+)$ ./pretty.php?pretty=$1 [QSA]
ErrorDocument 404 /404.php
Run Code Online (Sandbox Code Playgroud)
pretty.php
// First check if passed pretty parameter is a username.
if(checkIfUserByUsername($_GET['pretty']))
{
$_GET['username'] = $_GET['pretty'];
include("USERNAME.php");
// If not a username, check to see if it's an event.
}else if(checkIfStreamByPretty($_GET['pretty'])){
$_GET['id'] = getStreamIdFromPretty($_GET['pretty']);
include("GROUP.php");
}else{
// If it's none, redirect to the 404 page.
include("404.php");
}
Run Code Online (Sandbox Code Playgroud)
我想提供更多我想要完成的内容,我正在创建页面模板文件,其中一个用于配置文件,其中一个用于组,就像你在我的.htaccess和pretty.php上面看到的那样我在那里然后使用页面模板文件的顶部
$user_id = getIdFromUsername($_GET['username']);
$_GET['user_id'] = $user_id;
Run Code Online (Sandbox Code Playgroud)
我真的很想弄清楚如何以一种允许它使用多个主题的方式来解决这个问题.在这一点上寻找任何类型的解决方案.
如果我理解你想要实现的目标,有几种方法可以解决这个问题.您可能不需要使用任何重写.
最简单的解决方案是将"pretty.php"代码添加到Wordpress模板中的header.php文件中.这将加载到网站前端的每个请求.如果您只需要在特定页面上加载代码,只需使用is_page检查您正在使用的页面.
例如,在header.php中:
if(is_page("groups")){
if(checkIfUserByUsername($_GET['pretty']))
{
// the rest of your code
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想写一个Wordpress插件,你只需要在你的插件中使用"add_action":
add_action('wp_head', 'function_containing_your_code');
Run Code Online (Sandbox Code Playgroud)
同样,这将加载到每个页面上,因此只需检查您是否在要加载代码的页面上.
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_head https://developer.wordpress.org/reference/functions/is_page/
此外,您可以编辑.htaccess文件,就像您可以编辑用户有权编辑的任何文件一样.假设您正在运行Apache服务器,您的.htaccess文件需要具有允许编辑文件的权限,您可以使用PHP编辑该文件.例如:
$file = fopen("/path/to/.htaccess", "a+");
fwrite($f, "RewriteRule ^(\w.+)$ ./pretty.php?pretty=$1 [QSA]");
fclose($f);
Run Code Online (Sandbox Code Playgroud)
许多人会说PHP写入.htaccess文件是一种安全风险.我会说在某些情况下这是真的,但肯定并非总是如此.在允许PHP写入.htaccess文件之前让您了解权限.