【问题标题】:How to rewrite the url by htaccess and also replace the underscore with dash如何通过 htaccess 重写 url 并用破折号替换下划线
【发布时间】:2015-12-05 20:47:30
【问题描述】:

我有一个在 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

【问题讨论】:

  • 是您在 wordpress 中的网站

标签: php wordpress .htaccess mod-rewrite


【解决方案1】:

像这样转换网址

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

其他页面类似

【讨论】:

  • 感谢 piyush,但它不起作用。当我这样做时,它会将我带到 404。jQuery 使用像 "?id=why_us" 这样的 id 来显示隐藏 div
【解决方案2】:

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

RewriteEngine On
RewriteBase /

之后是另一个网址:-

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

htaccess 规则将是:-

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

也许对你有帮助。

【讨论】:

  • 你的第一条规则是什么?
  • 现在对你有帮助吗?
  • 无论你在 about-us 之后传递了什么,它都会作为 id 传递到该页面。
  • 你不行吗?
猜你喜欢
  • 2011-12-24
  • 2010-11-19
  • 2012-12-26
  • 1970-01-01
  • 2013-04-01
  • 2013-03-22
  • 1970-01-01
  • 2013-01-24
  • 1970-01-01
相关资源
最近更新 更多