使用mod_rewrite使博客URL更加SEO友好

时间:2013-09-12 21:19:54

标签: php mysql apache .htaccess mod-rewrite

我正在尝试使用mod_rewrite将我的博客网址转换为更友好的SEO格式。我的所有文章都存储在一个简单的MySQL数据库中。每个博客文章网址如下所示:

http://www.test.com/blog?id=20&category=online%20php%20tutorials&pagename=how%20to%20customize%20functions

我设法使用mod_rewrite使它们看起来像这样:

http://www.test.com/blog/online-php-tutorials/how-to-customize-functions/20

以下是我在.htaccess文件中编写的代码:

RewriteRule ^blog/([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ /blog?id=$3&category=$1&pagename=$2 [L]

所以会发生这样的事情:当我点击URL http://www.test.com/blog/online-php-tutorials/how-to-customize-functions/20时,我的所有CSS和图像都没有加载,因为它试图从一个实际上不存在的目录中加载它们。如何在不创建包含我的网站CSS文件和图像的多个目录的情况下加载文件?

2 个答案:

答案 0 :(得分:2)

在路径中使用根标识符/。这将指向服务器的DocumentRoot。让我解释一下这是如何工作的

对于这样的图像:

<img src='test.jpg' /> 

浏览器/服务器会将其视为http://www.test.com/blog/online-php-tutorials/how-to-customize-functions/20/test.jpg',但如果您在路径前使用/

<img src='/test.jpg' />

它将在http://www.test.com/test.jpg

中查找

另一种技术

在文件中使用完整路径,例如:

<img src='http://test.com/test.jpg' />

答案 1 :(得分:0)

您希望CSS路径基于域的根目录。如果您的CSS文件位于http://example.com/includes/style.css,则应使用/includes/style.css来包含该文件,而不是includes/style.css