.htaccess - htaccess 重定向 index.php?p=my topic title to/my-topic-title

标签 .htaccess url redirect url-rewriting seo

我想使用 htaccess 更改我网站的 URL

URL currently: example.com/index.php?p=my-topic-title
I want to be like : example.com/my-topic-title 

我当前的 htaccess 文件:

    RewriteEngine on

RewriteRule ^p/([0-9أ-يa-zA-Z_.]+) index.php?p=$1 [NC,L]

这段代码的结果是:

example.com/p/my-topic-title

我希望 url(没有 (/p/) )像 :example.com/my-topic-title

最佳答案

I get the solution for (spaces) ok but I want to change my URL (example.com/index.php?=my-topic-title) to (example.com/my-topic-title)

我假设您已经更改了应用程序中的 URL,并且您只需要重定向搜索引擎/用户以保持 SEO。

在这种情况下,您可以上面的重写之前执行类似以下操作:

 RewriteCond %{QUERY_STRING} ^p=([^&]{3,})
 RewriteRule ^index\.php$ /%1 [QSD,R,L]

这假设页面标题至少有 3 个字符。 %1 反向引用包含前面 CondPattern 中捕获的子模式。 QSD 标志 (Apache 2.4+) 是从重定向的 URL 中删除查询字符串所必需的。

关于.htaccess - htaccess 重定向 index.php?p=my topic title to/my-topic-title,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53469873/

相关文章:

url - Extjs 4 - 如何从当前加载的网格商店中获取带有参数的完整 url?

php - 从 URL txt 获取文本

javascript - 页面重定向脚本给了我一个重定向循环

wordpress - 带有域掩码的 htaccess 重定向 Wordpress 站点不起作用

.htaccess - Ionic - XMLHttpRequest 无法加载

.htaccess - 访问新的 php 文件

Android在线XML解析问题-SAX Parser

mod-rewrite - 如何在 JBoss 服务器上重写 URL?

reactjs - 仅当未通过 @azure/msal-react 进行身份验证时才重定向 onLoad

html - 如何在 Chrome 中查看 mime 类型?它在 "document"选项卡下显示 "Network"