简体   繁体   English

.htaccess RewriteRule在Wordpress上不起作用

[英].htaccess RewriteRule Not Working on Wordpress

A client asked me to built them a site with WordPress. 一位客户要求我用WordPress建立一个网站。 I'm not very good at WordPress so the solution may already be on here, but I've read about a dozen SO questions and haven't figured out an answer yet so I'd thought I'd post my own. 我不是很擅长WordPress,所以解决方案可能已经在这里,但是我已经读了十几个SO问题,还没有找到答案,所以我想我会发表自己的问题。

This is my .htaccess file: 这是我的.htaccess文件:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^podcast/([^/]+)$ /podcast?slug=$1 [QSA,L] #I added this

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

With my file like this, it redirects me to my 404 page. 使用这样的文件,它会将我重定向到404页面。 Even though the URL is correct ( /podcast/word ) the browser console says it's a 404. 即使URL是正确的( /podcast/word ),浏览器控制台仍说它是404。

I then assumed if I added an "R=301" flag, it would work around that issue. 然后,我假设如果添加了“ R = 301”标志,它将可以解决该问题。 But if I do this: 但是,如果我这样做:

RewriteRule ^podcast/([^/]+)$ /podcast?slug=$1 [R=301,QSA,L]

Then my browser just takes me to /podcast?slug=word . 然后,我的浏览器将我带到/podcast?slug=word

I'm not sure if this is caused by my .htaccess or if it's caused by the WordPress 404 file. 我不确定这是由我的.htaccess引起还是由WordPress 404文件引起。 On the off chance anybody wants to see it, my 404.php file is: 如果有人希望看到它,我的404.php文件是:

<?php get_header();  ?>

<section class="white">
    <div class="wrapper internal">
       <!--generic error message-->
    </div>
</section>

<?php get_footer(); ?>    

My .htaccess file worked fine when using Wolf CMS, but not with WordPress. 使用Wolf CMS时,我的.htaccess文件工作正常,但与WordPress无关。 Being as I'm still learning WordPress, any help is greatly appreciated. 由于我还在学习WordPress,因此非常感谢您的帮助。

EDIT 编辑

I'm using the slug to reference a podcast from an RSS feed. 我正在使用Slug引用RSS源中的播客。 I'm looking to turn http://example.com/podcast/?slug=example-podcast into http://example.com/podcast/example-podcast , but whenever I do that I get a 404. 我希望将http://example.com/podcast/?slug=example-podcast转换为http://example.com/podcast/example-podcast ,但是无论何时我都会得到404。

Try to replace your .htaccess with this code. 尝试用此代码替换您的.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

Example: if I had my wordpress inside a folder named 'a' as a sub directory then I will edit my .htaccess like this 示例:如果我将wordpress放在名为“ a”的文件夹中作为子目录,那么我将像这样编辑.htaccess

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

# END WordPress

Also you can set permalink to Post Name. 您也可以将永久链接设置为帖子名称。 This will fix if your url are define by a post name instead of php type of url. 如果您的网址是由帖子名称而不是php类型的网址定义的,则可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM