WordPress:自定义重写规则 - .htaccess 还是functions.php?

发布于 2024-09-11 22:34:49 字数 1136 浏览 1 评论 0原文

我要疯了。我唯一想做的就是将这些规则添加到我正在开发的网站中:

fastigheter-spanien/x    property/X&lang=sv
fastigheter-usa/x    property/X&lang=sv
properties-spain/X    property/X&lang=en
properties-usa/X    property/X&lang=en

其中 X 是帖子的名称。这些都是自定义帖子类型。我最终遇到这种情况的原因是,我使用多种语言,但希望客户不必为每篇文章创建多个帖子(他们有很多选项和图像,必须重新发布它们实际上是不可行的)问题)。 我研究过在functions.php 中创建rewrite_rules 并修改.htaccess 文件,但无法使其正常工作。这些重写非常简单,不需要使用任何函数。我不能只修改 .htaccess 文件吗?

试过了,

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !^/fastigheter-spanien/([^\./]+)
RewriteCond %{REQUEST_URI} !^/fastigheter-usa/([^\./]+)
RewriteCond %{REQUEST_URI} !^/properties-spain/([^\./]+)
RewriteCond %{REQUEST_URI} !^/properties-usa/([^\./]+)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteRule ^fastigheter-spanien/([^\./]+)$ property/$1 [L]
RewriteRule ^fastigheter-usa/([^\./]+)$ property/$1 [L]
RewriteRule ^properties-spain/([^\./]+)$ property/$1&lang=sv [L]
RewriteRule ^properties-usa/([^\./]+)$ property/$1&lang=sv [L]

哪个只给了我一个 404。有什么想法吗?

I'm going crazy. The only thing I want to do is add these rules to the site that I'm developing:

fastigheter-spanien/x    property/X&lang=sv
fastigheter-usa/x    property/X&lang=sv
properties-spain/X    property/X&lang=en
properties-usa/X    property/X&lang=en

Where X is the name of the post. These are all custom post types. The reason I ended up with this situation is that I use multiple languages but wanted the client not having to create more than one post for each article (they have a lot of options and images, having to re-publish them is practically out of the question).
I've looked into creating rewrite_rules in functions.php and also modifiying the .htaccess file but haven't been able to get it to work. These rewrites are so simple they shouldn't need to use any functions. Can't I just adapt the .htaccess file?

Tried

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !^/fastigheter-spanien/([^\./]+)
RewriteCond %{REQUEST_URI} !^/fastigheter-usa/([^\./]+)
RewriteCond %{REQUEST_URI} !^/properties-spain/([^\./]+)
RewriteCond %{REQUEST_URI} !^/properties-usa/([^\./]+)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteRule ^fastigheter-spanien/([^\./]+)$ property/$1 [L]
RewriteRule ^fastigheter-usa/([^\./]+)$ property/$1 [L]
RewriteRule ^properties-spain/([^\./]+)$ property/$1&lang=sv [L]
RewriteRule ^properties-usa/([^\./]+)$ property/$1&lang=sv [L]

Which just gives me a 404. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒猫 2024-09-18 22:34:49

您的重写应该可以正常工作,但 WordPress 看不到您的修改。原因是 WordPress 从 $_SERVER['REQUEST_URI'] 读取,其中始终包含原始请求 URI,而不是从 生成的请求 URI。 mod_rewrite 操作。

我对实际使用 WordPress 并不熟悉,但我查看了一些其他问题的源代码,似乎它也会尝试使用 $_SERVER['PATH_INFO'],如果已提供。因此,您可以尝试这样做:

AcceptPathInfo On
RewriteEngine On
RewriteBase /

RewriteRule ^fastigheter-(spanien|usa)/([^\./]+)$ property/$2&lang=sv
RewriteRule ^properties-(spain|usa)/([^\./]+)$    property/$2&lang=en

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php/$0 [L]

我发现使用 PATH_INFO 是有问题的(并且通常在每个目录上下文中不起作用,尽管我认为我还没有确定原因) ,并且通常建议反对它。

您的另一个选择还包括在 PHP 方面进行一些工作。您可以执行类似于 Anraiki 建议的操作,或者您可以直接告诉 $_SERVER['REQUEST_URI'] 是您所期望的。

如果 mod_rewrite 执行重定向,则模块看到的 REQUEST_URI 将存储在服务器的 REDIRECT_URL 变量中。因此,稍微调整一下重写规则集,我们可以这样做:

RewriteEngine On
RewriteBase /

# Force L on these rules to cause the REQUEST_URI to be changed (so that
# REDIRECT_URL will end up with the rewritten value we want)
RewriteRule ^fastigheter-(spanien|usa)/([^\./]+)$ property/$2&lang=sv [L]
RewriteRule ^properties-(spain|usa)/([^\./]+)$    property/$2&lang=en [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

然后,在 WordPress 处理的早期某个位置(例如在 index.php 中,因为您可能不需要使用以下命令更新该文件)后续的 WordPress 版本),您可以执行以下操作:

if (!empty($_SERVER['REDIRECT_URL']))
    $_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_URL'];

很可能有一种方法可以使用永久链接设置本身来定义您在 WordPress 中的行为,因为它在解析请求时会迭代该规则列表,但我并不完全清楚关于这个的细节,因为我自己从未使用过它。如果可能的话,我会认为这是处理这种情况的推荐方法,但无论哪种方式,希望这些选项之一都能为您服务。

Your rewrites should be working correctly, but WordPress doesn't see your modifications. The reason for this is that WordPress reads from $_SERVER['REQUEST_URI'], which will always contain the original request URI, instead of the resulting one from the mod_rewrite operations.

I'm not familiar with actually using WordPress, but I have looked at some of the source code for some other questions, and it seems it will also attempt to use $_SERVER['PATH_INFO'], if it's provided. Consequently, you can try this:

AcceptPathInfo On
RewriteEngine On
RewriteBase /

RewriteRule ^fastigheter-(spanien|usa)/([^\./]+)$ property/$2&lang=sv
RewriteRule ^properties-(spain|usa)/([^\./]+)$    property/$2&lang=en

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php/$0 [L]

I find that using PATH_INFO is problematic though (and typically won't work in a per-directory context, although I don't think that I've determined why yet), and generally recommend against it.

Your other option involves working a bit on the PHP side as well. You can do something similar to what Anraiki suggested, or you can just tell $_SERVER['REQUEST_URI'] to be what you're expecting.

If mod_rewrite performs a redirection, the REQUEST_URI as the module sees it is stored in the server's REDIRECT_URL variable. So, adjusting the rewrite ruleset a bit, we can do this:

RewriteEngine On
RewriteBase /

# Force L on these rules to cause the REQUEST_URI to be changed (so that
# REDIRECT_URL will end up with the rewritten value we want)
RewriteRule ^fastigheter-(spanien|usa)/([^\./]+)$ property/$2&lang=sv [L]
RewriteRule ^properties-(spain|usa)/([^\./]+)$    property/$2&lang=en [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Then, somewhere early on in the WordPress processing (such as in index.php, since you shouldn't likely need to update that file with subsequent WordPress releases), you could do the following:

if (!empty($_SERVER['REDIRECT_URL']))
    $_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_URL'];

There may very well be a way to define your behaviour in WordPress using the permalink settings themselves, since it does iterate over that rule list in parsing the request, but I'm not entirely clear on the details of this as I've never used it myself. If this is possible, I would consider it the recommended way of handling this situation, but either way hopefully one of these options should get things working for you.

涙—继续流 2024-09-18 22:34:49

如果 .htaccess 文件的所有其他方法都失败了,那么还有一种替代解决方案可以挂钩“通过插件”。

这是我的一个插件的片段。

<?php 
function fancy_url_hook($var='REQUEST_URI')
{
    if (!in_array($var, array('REQUEST_URI', 'PATH_INFO'))) $var = 'REQUEST_URI';
    $req = $_SERVER[$var];

    if (($var != 'PATH_INFO') && isset($_SERVER['PATH_INFO'])) {
        fancy_url_hook('PATH_INFO');
    }
    $explodeURL = array_slice(explode('/',$req),1,5);

    global $language;

    switch($explodeURL) {
        case 'fastigheter-spanien': $url = 'sv'; break;
        case 'fastigheter-usa': $url = 'sv'; break;
        case 'properties-spain': $url = 'en'; break;
        case 'properties-usa': $url = 'en'; break;
    }

    $language = $url;

}

function source_function()
{
    global $language;
    $_GET['lang'] = $language;

}

add_action('init', 'fancy_url_hook');
add_action('init', 'source_function');
?>

尽管如此,它需要一些编辑,并且必须直接从“插件”挂钩。所以可能需要一个插件。

不过,您是否尝试过在编辑 .htaccess 文件后禁用所有插件?

If all else fails with the .htaccess file then there is a alternative solution to hooking in "by plugin"

Here is a snippet from one of my plug-ins.

<?php 
function fancy_url_hook($var='REQUEST_URI')
{
    if (!in_array($var, array('REQUEST_URI', 'PATH_INFO'))) $var = 'REQUEST_URI';
    $req = $_SERVER[$var];

    if (($var != 'PATH_INFO') && isset($_SERVER['PATH_INFO'])) {
        fancy_url_hook('PATH_INFO');
    }
    $explodeURL = array_slice(explode('/',$req),1,5);

    global $language;

    switch($explodeURL) {
        case 'fastigheter-spanien': $url = 'sv'; break;
        case 'fastigheter-usa': $url = 'sv'; break;
        case 'properties-spain': $url = 'en'; break;
        case 'properties-usa': $url = 'en'; break;
    }

    $language = $url;

}

function source_function()
{
    global $language;
    $_GET['lang'] = $language;

}

add_action('init', 'fancy_url_hook');
add_action('init', 'source_function');
?>

Although, it will need some editing and it has to directly hook in from a "plugin". So a plug-in may be necessary.

Although, have you tried disabling all plug-in after editing the .htaccess file?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文