将具有单个非数字参数的动态 url 重定向到名称由参数值文本组成的静态无扩展根级别 url

Redirect dynamic url with single non-numeric parameter to static extension-less root level url with name consisting of parameter value text

我有一个 .htaccess 允许我使用 .php 个文件 urls 而无需扩展名:

    # if not a directory and .php file is present then add .php extension
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/.php -f
    RewriteRule ^(.+?)/?$ .php [L]

我正在尝试创建一个控制器 page.php 文件,它将允许我根据参数值生成多个动态页面,例如:

https://www.example.com/page.php?pid=some-page-slug-1

https://www.example.com/page.php?pid=some-page-slug-2

等等。

我想用下一种方式把上面的动态链接变成静态链接:

https://www.example.com/page.php?pid=some-page-slug-1

转为:

https://www.example.com/some-page-slug-1

我试图修改我在互联网上找到的一些模板,但它不起作用:

RewriteRule (.*)\.php$ page.php?pid=

以 SEO 友好的方式将具有单个非数字参数的动态 url 重写为名称由参数值文本组成的静态无扩展根级别 url 的正确标记是什么?

您可以试试这些规则:

RewriteEngine On

# if not a directory and .php file is present then add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/.php -f
RewriteRule ^(.+?)/?$ .php [L]

# singe parameter static URL handlinng
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ page.php?pid= [L,QSA]