當我的客戶端向網站添加頁面時,新的頁面名稱應該附加到RewiteRule正則表達式中。因此,例如fwrite(),我希望PHP使用從數據庫收回的值更改RewiteRule正則表達式。如果可以這樣做,這個過程中是否存在缺陷?是否可以使用數據庫值構建RewriteRule正則表達式?
編輯:在PHP腳本處理將是解決辦法,如果有would'nt更給它... 第一個域/ index.php頁面=頁面名稱是301重定向到「域/網頁」 來警告訪問者這個頁面永久移動 - (這是舊的 公共位置的URL,應該給這301)。然後請求 ,如「domain/pagename」(新的公共位置),將默認爲 ,內部重寫爲domain/index.php?page = pagename,其中 驗證發生,並且404無效時給出。但只是 關鍵字(頁面= pagename的「頁面」部分)是靜態的,可以驗證 ,並且會直接從.htaccess中提供404。現在, 請求像domain/index.php?page = crap將首先很好地給出一個301 像有效的domain/index.php?page = pagename那樣,並且只有當 到達index.php時纔可以被識別爲廢話。因此,仍然需要從數據庫中獲取頁面名稱到.htaccess中。
這是內容提供一些背景,這個問題.htacces的樣本:
ErrorDocument 404 http://localhost/testsite/404.php
RewriteEngine on
RewriteBase /testsite/
## block craprequests without extension like domain/crap > 404
# The requests domain/pagename that do not go to existing pages, will now be redirected with a 302 to index.php?page=pagename and only then give a 404 through the errorcheck in the code.
# This should be done here, with a RewriteCond regex with database content
RewriteCond %{REQUEST_URI} !404.php$
RewriteRule .* 404.php [R=404,L]
## block-direct-queries ##
RewriteCond %{QUERY_STRING} !marker=1$
RewriteCond %{QUERY_STRING} page=(.*)
RewriteRule ^.*$ %1? [R=301,L]
## strip-extensions ##
RewriteCond %{QUERY_STRING} !.
RewriteCond %{REQUEST_URI} !404.php$
RewriteRule ^([\w+%*\d*\+*\-*]+)\.(php[\s]{0,3}|htm[\s]{0,3}|html[\s]{0,3})$ $1 [R=301,L]
## put-querystring
RewriteRule ^([\w\-_]+)\/?$ index.php?page=$1&marker=1 [L]
是的,你可以做到這一點(這並不容易,但你可以做到) – Oussama 2012-08-14 11:32:25
請詳細說明。包括一個'.htaccess'樣本,以及樣本在修改後的樣子。 – Tomalak 2012-08-14 11:34:39
聲音就像通用重寫規則,通過PHP控制器腳本路由所有內容就是答案,而不是每次修改站點佈局時都修改.htaccess(它也具有相當高的安全性)。 – DaveRandom 2012-08-14 11:35:22