簡體   English   中英

用於SEO友好URL的mod_rewrite

[英]mod_rewrite for SEO friendly URLs

我有一個CMS,可以處理用CMS創建的SEO友好URL。 因此,將index.php的擴展名(例如www.url.com/index.php?id=slug都很好地更改為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。

更改

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]

您需要在開頭添加item以匹配您給出的示例,並對字符類進行一些更改。 嘗試:

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

這將匹配以下項的URL:item / [字母,數字和下划線的字符串] / [字母,數字和下划線的字符串] .html

如果您的ID僅是數字,則可以從第一個字符類中刪除字母。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM