如何通过htaccess重写url,并用破折号替换下划线


How to rewrite the url by htaccess and also replace the underscore with dash

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

目前,我有这个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

转换类似的URL

/index.php?p=测试

/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]

与其他页面类似,

您可以尝试将下划线转换为短划线:-

RewriteEngine On
RewriteBase /

之后为另一个网址:-

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

htaccess规则将是:-

RewriteRule ^about-us/(.*)$ about-us/?id=$1 [QSA,L]

它可能会对你有所帮助。