php - 使用 htaccess 创建 SEO 友好的 url

标签 php .htaccess url mod-rewrite seo

我正在尝试重写网站的 url,我应该提到 index.php 现在的工作方式是获取 p(页面)参数并包含适当的文件。

所以请求一个页面是这样的:

www.domain.com/index.php?p=home
www.domain.com/index.php?p=search
www.domain.com/index.php?p=profile
www.domain.com/index.php?p=contact

我找到了如何为此创建重写规则:

RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?p=$1

所以现在 www.domain.com/home 会给我主页

但我还需要为此提供一个友好的 url

www.domain.com/index.php?p=profile&id=20

to

www.domain.com/profile/20/profile-friendly-name

or better

www.domain.com/profile/profile-friendly-name

*profile-friendly-name 是指公司名称

要求:

  1. 为所有页面设置友好的 URL,例如/home,/contact

  2. 为个人资料页面提供一个特别友好的 URL url 中的个人资料名称

我的问题:

  1. 我如何向具有现有 url 格式 (index.php?p=profile&id=20) 的 url 添加配置文件友好名称?

  2. 我能不能像我的名字一样只有一个唯一的名字(没有 id) 例子?

  3. 如果我设法做到这一点,我是否必须更改所有现有的 url 在站点内(链接、图像、文件的 URL)给友好的 格式?

  4. 我注意到在应用第一个 RewriteRule 之后一些 css 不包括样式表和 js。怎么了?

最佳答案

RewriteRule ^profile/([0-9]+)/([A-Za-z0-9-]+)/?$ index.php?p=profile&id=$1

适用于:

www.domain.com/index.php?p=profile&id=20

to

www.domain.com/profile/20/profile-friendly-name

关于php - 使用 htaccess 创建 SEO 友好的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26285463/

相关文章:

php - 路由分页链接的问题

PHP file_get_contents 使用变量

regex - example.com/en/blabla 的行为类似于 example.com/blabla?lang=en

php - .htaccess 重定向,MIME 错误

java - 我可以在 400 错误后使用 Java 的 SAX 解析 XML 数据吗?

javascript - 如何强制 href 链接为绝对链接?

php - 存储和检索翻译文本的最快方法 - 数据库还是平面文件?

php - 为什么 symfony DOMCrawler 对象无法在依赖的 phpunit 测试之间正确传递?

.htaccess RewriteRule subsubdomain.subdomain.domain.tld 到 subdomain.domain.tld/subsubdomain

Asp.net 从 Https 重定向到 Http