【发布时间】:2012-06-09 01:22:59
【问题描述】:
我有一个 CMS 可以处理使用 CMS 创建的 SEO 友好 URL。所以任何像www.url.com/index.php?id=slug 这样的index.php 的扩展都可以很好地更改为www.url.com/slug。
但是,我还有一千多个使用未连接到我的 CMS 的 MYSQL 查询字符串创建的页面。所以我有:
www.url.com/item.php?id=001234&pg=author-title
生成页面需要 id 和 pg 中的信息。我想要的是这些页面网址:
www.url.com/item/001234/author-title.html
我查看了许多 mod_rewrite 教程,寻找所有方法来做到这一点,但我似乎无法让它发挥作用。
这是我的 .htaccess(请记住,这里的一些内容是由我的 CMS 使用和生成的:
编辑:好的,根据到目前为止的答案,我已将 .htaccess 更改为以下内容:
# Use PHP5 as default
AddHandler application/x-httpd-php5 .php
AddDefaultCharset UTF-8
# File modified on Dec 5 11:28:31 2011 by server
# For security reasons, mod_php is not used on this server. Use a php.ini file for php directives
# php_value default_charset "UTF-8"
Options -Indexes
Options +FollowSymLinks
# blocks direct access to the XML files - they hold all the data!
<Files ~ "\.xml$">
Order allow,deny
Deny from all
Satisfy All
</Files>
<Files sitemap.xml>
Order allow,deny
Allow from all
Satisfy All
</Files>
RewriteEngine on
# Usually it RewriteBase is just '/', but
# replace it with your subdirectory path
RewriteBase /
# Usually it RewriteBase is just '/', but
# replace it with your subdirectory path
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?item/([0-9]+)/([a-zA-Z0-9_]+).html$ item.php?id=$1&pg=$2 [QSA,L]
编辑:根据反馈更改 .htaccess 后,重写的 URL 仍然没有产生预期的结果。我仍然收到 404。
【问题讨论】:
-
尝试在第一个 RewriteRule 之前添加一个
^否则我认为它只会匹配以一个或多个字母或数字结尾的任何 URL。 -
在我的 CMS 页面的 RewriteRule 之前添加 ^ 会破坏该功能。
-
我已经在这工作了几个小时,仍然没有想出任何有效的方法。
-
添加
^真的不应该破坏任何东西,因为它应该是一行的开始。也许您的 RewriteBase 不正确?如果你能给我你正在使用的实际 URL,那会有所帮助。
标签: php mysql .htaccess mod-rewrite seo