【问题标题】:mod_rewrite for SEO friendly URLs用于 SEO 友好 URL 的 mod_rewrite
【发布时间】: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


【解决方案1】:

改变

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L] #passes to CMS
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+).html$ item.php?id=$1&pg=$2 [QSA,L] #passes to your stuff

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?item/([a-zA-Z_]+)/([a-zA-Z_]+).html$ item.php?id=$1&pg=$2 [QSA,L]
# conditions need to be repeated
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L]

始终将更具体的规则置于更一般的规则之上。

除非您的商品 ID 使用字母,否则请使用:

RewriteRule ^/?item/([0-9]+)/([a-zA-Z_]+).html$ item.php?id=$1&pg=$2 [QSA,L]

【讨论】:

    【解决方案2】:

    您需要在开头添加item 以匹配您提供的示例并对您的角色类进行一些更改。试试:

    RewriteRule ^item/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)\.html$ item.php?id=$1&pg=$2 [QSA,L]
    

    这将匹配表单项/[由字母、数字和下划线组成的字符串]/[由字母、数字和下划线组成的字符串].html 的 URL

    如果您的 ID 只是数字,您可以从第一个字符类中删除字母。

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 1970-01-01
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 2012-02-25
      • 2015-02-14
      • 2010-12-03
      相关资源
      最近更新 更多