WordPress 自定义 mod_rewrite - 完全菜鸟?

发布于 2024-12-12 03:05:03 字数 715 浏览 0 评论 0原文

我正在尝试为我的 WordPress 安装创建一个自定义 mod_rewrite 规则,我已经为我的页面结构创建了一个自定义规则,我想保留它。

我想要制定的新自定义规则是每当有一个 get 变量时,例如: www.mysite.com/?profile=用户名

我想让它看起来像: www.mysite.com/用户名

这是我当前的 .htaccess 文件:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

我查看了 php.net 和许多关于一般情况下以及专门针对 WordPress 的 mod 重写的教程,但这一切都只是直接过去我的头,我不明白了。 如果有人能给我一些帮助,让我在这方面取得一些进展,我真的很感激,我真的很想了解 mod_rewrite。

我假设我需要创建一个新的 RewriteRule,但据我所知。

谢谢 坦率

I'm trying to make a custom mod_rewrite rule for my wordpress installation, I've already got a custom rule for my page structure which i would like to preserve.

The new custom rule i'd like to make is whenever there's a get variable like: www.mysite.com/?profile=username

I want to make it look like: www.mysite.com/username

This is my current .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

I've looked at php.net and many tutorials about mod rewriting in general and specifically for wordpress, but its all just going straight over my head, I don't understand it.
If anyone could give me some help to get some progress with this i'd really appreciate, i really want to get my head around mod_rewrite.

I'm assuming i need to create a new RewriteRule, but that's as far as i get.

Thanks
Frank

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

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

发布评论

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

评论(1

差↓一点笑了 2024-12-19 03:05:03

至少可以说,实现你想要实现的目标是困难的。它需要一个额外的 PHP 文件来解析传入的请求,因为 .htaccess 无法知道 URL 的 USERNAME 部分是否实际上是用户名。

我提到的 PHP 文件将从数据库加载用户表,并将 $_GET['profile'] 与条目进行比较,如果匹配,则重写为 /?profile=USERNAME

如果没有匹配,它只是传递到 index.php 。

但是,这里有一个更简单的解决方案,它保留了您想要的很多内容:

RewriteRule ^profile/([^/]+)$ ?profile=$1 [L]

它的作用是将 /profile/username 重写为 /?profile=username

它不是正是您正在寻找的,但实施起来要容易得多。该规则应直接位于 .htaccess 文件中的 RewriteRule ^index\.php$ - [L] 下方。

如果您有任何疑问,我非常乐意解释这里发生的一切。

Accomplishing what you want to accomplish is difficult, to say the least. It requires an additional PHP file to parse incoming requests because .htaccess has no way to know if the USERNAME part of the URL is actually a username or not.

The PHP file I mention would load the users table from the database and compare $_GET['profile'] to the entries and, on match, rewrite to /?profile=USERNAME

If there's no match, it simply passes on to index.php.

However, there's a much easier solution here, which preserves a lot of what you want:

RewriteRule ^profile/([^/]+)$ ?profile=$1 [L]

What this does is rewrite from /profile/username to /?profile=username

It's not exactly what you're looking for, but it's much easier to implement. The rule should go directly below RewriteRule ^index\.php$ - [L] in the .htaccess file.

If you have any questions, I'm more than happy to explain what all is happening here.

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