Wordpress RewriteRule不起作用

时间:2015-11-04 14:04:49

标签: wordpress apache .htaccess mod-rewrite

这是我当前的.htaccess文件内容:

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

我想改变:

http://example.com/search/?location=&radius_key=sydney/&keyword=&geolocation=

为:

http://example.com/massage/sydney/

我修改了脚本如下,但它没有更改我的URL。你能指导我吗?任何一种解决此问题的方法都非常受欢迎。感谢

# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/massage/(.*)$ /search/? location=&radius_key=$1&keyword=&geolocation=
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

2 个答案:

答案 0 :(得分:0)

如果您希望更改浏览器地址栏中显示的URL,则必须激活modrewrite中的“R”标志,以激活重定向。

所以你会写一些像

这样的东西
RewriteRule /search/\?.*location=([a-z0-9_.-]+)&.* geolocation=$1 [R=301,L]

我建议将配置拆分为两部分,以保持原始Wordpress配置不被修改。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /search/\?.*location=([a-z0-9_.-]+)&.* geolocation=$1 [R=301,L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

答案 1 :(得分:0)

您需要稍微改变一下规则。这应该允许您使用新的URL以及您的wordpress规则。

 # BEGIN WordPress
 <IfModule mod_rewrite.c>
 Options +FollowSymlinks -MultiViews
 RewriteEngine On
 RewriteBase /

 RewriteCond %{REQUEST_FILENAME} -f [OR]
 RewriteCond %{REQUEST_FILENAME} -d
 RewriteRule ^ - [L]

 RewriteCond %{THE_REQUEST} ^GET\ /search/\?location=&radius_key=([^\ &]+)&keyword=&geolocation=
 RewriteRule ^ /massage/%1? [R=301,L]

 RewriteRule ^massage/([^/]+)/?$ /search/?location=&radius_key=$1&keyword=&geolocation= [L]

 RewriteRule ^index\.php$ - [L]
 RewriteRule . /index.php [L]
 </IfModule>
 # END WordPress

让我知道这对你有什么用。