使用htaccess创建SEO友好的URL

时间:2014-10-09 18:23:39

标签: php .htaccess url mod-rewrite seo

我正在尝试重写网站的网址,我应该提一下,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 会给我一个主页

但我还需要一个友好的网址

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

*个人资料友好名称是指公司名称

要求:

  1. 为所有网页提供友好的网址,例如/ home,/ contact

  2. 要为个人资料页面添加特定的友好网址 网址中的个人资料名称

  3. 我的问题:

    1. 如何使用现有的url格式(index.php?p = profile& id = 20)为网址添加个人资料友好名称?

    2. 我可以只有一个唯一的名字(没有id),就像我的上一个 示例

    3. 如果我设法这样做,我是否必须更改所有现有网址 在网站内(链接,网址到图片,到文件)友好 格式?

    4. 我注意到在应用第一个RewriteRule后有些css 样式表和js不包括在内。有什么问题?

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

Ex 1: 使用htaccess网址的https://example.com/books.php?bookName=php将这样写:https://example.com/php

您可以使用以下.htaccess文件中给出的代码来完成此操作:

RewriteEngine On
RewriteRule ^([^/\.]+)/?$ books.php?bookName=$1

Ex 2:

使用htaccess网址的

https://example.com/books.php?bookName=php&&chapter=array的写法如下 https://example.com/php/array

您可以通过使用下面给出的.htaccess文件中的代码来获得:

RewriteEngine On
RewriteRule ^([^/\.]+)/([^/\.]+)?$ books.php?bookName=$1&chapter=$2
相关问题