我想使用 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/