.htaccess 中的 SEO 友好 URL 命令

发布于 2024-12-15 09:27:03 字数 336 浏览 3 评论 0原文

我有一个如下所示的动态页面:

www.sitedomain.com/page.php?id=30&name=about_our_company

我想通过如下内容使我的网站链接 SEO 友好:

www.sitedomain.com/about_our_company.html

或者

www.sitedomain.com/about_our_company

我的问题是: .htaccess 文件中应该有什么正则表达式/代码?

谢谢

I have a dynamic page that looks like the following:

www.sitedomain.com/page.php?id=30&name=about_our_company

I want to make my website links SEO friendly by having something like the following:

www.sitedomain.com/about_our_company.html

Or

www.sitedomain.com/about_our_company

My question is: what regex/code I should have in the .htaccess file?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜嗑 2024-12-22 09:27:03

此 ofc 的固定 ID 为 30

RewriteRule ^about_our_company/?$  /page.php?name=$1&id=30 [NC,L]

This ofc has a fixed id of 30.

RewriteRule ^about_our_company/?$  /page.php?name=$1&id=30 [NC,L]
森林很绿却致人迷途 2024-12-22 09:27:03

由于 /about_our_company 不包含 ID,因此不可能正确创建 ID。

我在自己的 CMS 中执行此操作的方法是在 .htaccess 中包含类似的内容:

RewriteEngine on
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,L]

然后在 index.php 中,使用 $_REQUEST['page'] (假设是 PHP)变量来查找数据库中的右页详细信息

since /about_our_company doesn't include the ID, it's impossible to invent the ID correctly.

the way I do it in my own CMS is to have something like this in the .htaccess:

RewriteEngine on
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,L]

then in index.php, use the $_REQUEST['page'] (assuming PHP) variable to find the right page details in the database

您可以将其改进为更通用:

RewriteRule ^([^/]+)/([^/]+)?$  /page.php?id=$1&name=$2 [NC,L]

以下 URL 将全部匹配:

http://example.com/30/about_our_company
http://example.com/29/contact
http://example.com/10/our_work

You could improve this to be more generic:

RewriteRule ^([^/]+)/([^/]+)?$  /page.php?id=$1&name=$2 [NC,L]

The following URL's would all match:

http://example.com/30/about_our_company
http://example.com/29/contact
http://example.com/10/our_work
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文