如何通过htaccess重写网址,并用短划线替换下划线

时间:2015-12-05 09:31:39

标签: php wordpress .htaccess mod-rewrite

我有一个在apache服务器上运行的WordPress站点,我想以SEO友好的方式重写一些URL,所以如何做到这一点

目前,我有这个网址

www.example.com/about-us/?id=what_we_do_1
www.example.com/about-us/?id=why_us


我希望它像

www.example.com/about-us/what-we-do-1
www.example.com/about-us/why-us


并为此

www.example.com/media/?id=team
www.example.com/media/?id=abc_xyz


我想要这样的东西

www.example.com/media/team
www.example.com/media/abc-xyz

我目前的.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

2 个答案:

答案 0 :(得分:0)

转换

等网址

/index.php?p=test

<强> /index.php/test

RewriteEngine On
RewriteCond %{QUERY_STRING} p=(.+)
RewriteRule ^/?index.php$ /index.php/%1? [R,L]

因此在你的情况下

RewriteEngine On
RewriteCond %{QUERY_STRING} p=(.+)
RewriteRule ^/?about-us?$ /about-us/%1? [R,L]

与其他页面类似

答案 1 :(得分:0)

您可以尝试将下划线转换为破折号: -

try {
    //your code here
}
catch (Throwable e) {
    log.error("Error in Beanshell", e);
}

之后为另一个网址: -

1)www.example.com/about-us/?id=what_we_do_1

htaccess规则将是: -

RewriteEngine On
RewriteBase /

它可能对你有帮助。

相关问题