簡體   English   中英

.htaccess中的多個重寫規則以重定向到所需的頁面

[英]Multiple Rewrite Rules in .htaccess To Redirect at desired pages

我正在嘗試建立一個網站,我在其中創建永久鏈接,就像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的新手,因此,感謝您的幫助。

我假設.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]

暫無
暫無

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

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