你如何简单地将新的自定义重写添加到wordpress的.htaccess中?

时间:2021-08-08 19:19:57

I've added some extra functionality to my wordpress so that I can visit it with a variable and do extra stuff.

我已经为我的wordpress添加了一些额外的功能,以便我可以使用变量访问它并执行额外的操作。

The problem is, when I turn my ugly dynamic link into lovely permlink formatting in the .htaccess file, wordpress overrides it / ignores it. I've heard there's a way to do it, but the ways I try to do it based off what people have said still returns a 404 page regardless. I know that the file its pointing to works.

问题是,当我将丑陋的动态链接转换为.htaccess文件中可爱的permlink格式时,wordpress会覆盖它/忽略它。我听说有办法做到这一点,但我试图根据人们所说的方式做的方式仍然会返回404页面。我知道它指向的文件有效。

2 ways ppl say works but I've had no joy with:

ppl说2种方式有效,但我对此并不满意:

1) insert the rules above the #BEGIN wordpress part 2) use add_rewrite_rule() wordpress function somewhere

1)在#BEGIN wordpress部分上面插入规则2)在某处使用add_rewrite_rule()wordpress函数

Has anybody had any success with these methods? or other methods?

有没有人用这些方法取得任何成功?或其他方法?

Here's what my .htaccess file looks like

这是我的.htaccess文件的样子

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/ref/(.*)$ /index.php?ref=1&sid=$1 [NC]
</IfModule>


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /


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


</IfModule>

In my themes function.php I've also tried adding:

在我的主题function.php我也尝试添加:

add_rewrite_rule('/ref/(.*)$', 'index.php?ref=1&sid=$matches[1]','top');

With no success.

没有成功。

I've also tried the solutions over @ WordPress + mod_rewrite with no joy.

我也尝试过@ WordPress + mod_rewrite的解决方案,没有任何乐趣。

Please help! :)

请帮忙! :)

any ideas?

4 个解决方案

#1


This works - I just tested it - Note I added an L to the end of the RewriteRule

这工作 - 我只是测试它 - 注意我在RewriteRule的末尾添加了一个L.

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^/ref/(.*)$ /index.php?ref=1&sid=$1 [NC,L]

#wp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

#2


a new rule will always override the old ones

新规则将始终覆盖旧规则

try the following

尝试以下方法

<IfModule mod_rewrite.c>
RewriteEngine On
#wp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteRule ^/ref/(.*)$ /index.php?ref=1&sid=$1 [NC]


</IfModule>

#3


In .htaccess files you have to leave the leading slash in the patterns of the RewriteRule directive away. So try this:

在.htaccess文件中,您必须在RewriteRule指令的模式中保留前导斜杠。所以试试这个:

RewriteRule ^ref/(.*)$ index.php?ref=1&sid=$1 [NC]

#4


I wound up doing the following in php since the above solutions seemed to not work. Wordpress rulership over the .htaccess file is supreme:

由于以上解决方案似乎无效,我最终在php中执行了以下操作。 .htaccess文件上的Wordpress统治权是至高无上的:

if(strstr($_SERVER['REQUEST_URI'], '/ref/')) {

And from that have been able to do fairly much the same stuff. A pretty url that translates into something else.

从中可以做出相同的东西。一个漂亮的网址,转化为别的东西。

#1


This works - I just tested it - Note I added an L to the end of the RewriteRule

这工作 - 我只是测试它 - 注意我在RewriteRule的末尾添加了一个L.

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^/ref/(.*)$ /index.php?ref=1&sid=$1 [NC,L]

#wp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

#2


a new rule will always override the old ones

新规则将始终覆盖旧规则

try the following

尝试以下方法

<IfModule mod_rewrite.c>
RewriteEngine On
#wp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteRule ^/ref/(.*)$ /index.php?ref=1&sid=$1 [NC]


</IfModule>

#3


In .htaccess files you have to leave the leading slash in the patterns of the RewriteRule directive away. So try this:

在.htaccess文件中,您必须在RewriteRule指令的模式中保留前导斜杠。所以试试这个:

RewriteRule ^ref/(.*)$ index.php?ref=1&sid=$1 [NC]

#4


I wound up doing the following in php since the above solutions seemed to not work. Wordpress rulership over the .htaccess file is supreme:

由于以上解决方案似乎无效,我最终在php中执行了以下操作。 .htaccess文件上的Wordpress统治权是至高无上的:

if(strstr($_SERVER['REQUEST_URI'], '/ref/')) {

And from that have been able to do fairly much the same stuff. A pretty url that translates into something else.

从中可以做出相同的东西。一个漂亮的网址,转化为别的东西。