我正在尝试设置一个网站,我正在创建永久链接,就像wordpress一样,但是特定于网站。因此,当用户点击文章时,它应该是
http://www.sitename.com/url-slug
当用户点击目录时,它应该重定向这样的
http://www.sitename.com/category/category-slug
同样,当点击集合,用户,页面时,它应分别重定向,如
http://www.sitename.com/sets/set-slug
http://www.sitename.com/user/user-full-name
http://www.sitename.com/page/page-slug
以下是我在.htaccess中使用的代码,我非常肯定是不正确的
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*)$ ./single.php?id=$1
RewriteRule ^category/(.*)/ category.php?id=$1 [L]
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.sitename.com/$1 [R=301,L]
有人可以指导我出错的地方,我需要这些页面分别调用各自的页面,分别是single.php,category.php,sets.php,users.php和page.php来获取和显示数据。我是.htaccess的新手,所以,任何帮助都表示赞赏。
答案 0 :(得分:9)
我假设您的.htaccess中没有其他规则位于您网站的根目录中
RewriteEngine on
RewriteBase /
#if for an existing directory or file
RewriteCond %{SCRIPT_FILENAME} -d
RewriteCond %{SCRIPT_FILENAME} -f
#then do nothing
RewriteRule . - [L]
#otherwise
#if it is for a slug (assuming alphanumeric and dashes), send to single
RewriteRule ^([-a-zA-Z0-9]+)/?$ single.php?id=$1 [L]
#if a category
RewriteRule ^category/([-a-zA-Z0-9]+)/?$ category.php?id=$1 [L,NC]
#if a sets
RewriteRule ^sets/([-a-zA-Z0-9]+)/?$ sets.php?id=$1 [L,NC]
#if a sets
RewriteRule ^user/([-a-zA-Z0-9]+)/?$ users.php?id=$1 [L,NC]
#if a page
RewriteRule ^page/([-a-zA-Z0-9]+)/?$ page.php?id=$1 [L,NC]