[英]Simple mod_rewrite in existing website
我正在做一個網站(針對葡萄牙語國家),一次顯示一個短語。
我想讓網站更簡單的非動態地址變得更友好,更容易記住用戶。 所以我的問題是這個,並提前感謝所有可能的幫助:
nfrases.com/ -> completely random phrase (don't need changing this!)
nfrases.com/index.php?id_frase=2222 -> phrase with ID (wanted this to be nfrases.com/2222)
nfrases.com/tag.php?tag_nome=amor -> random phrase with tag_nome (want: nfrases.com/amor)
nfrases.com/tag.php?tag_nome=amor&id_frase=2222 -> specific phrase with tag=amor (want: nfrases.com/amor/2222)
我認為這是一個關於mod_rewrite的noob問題,但這正是我的意思! :D已經用我所掌握的一些知識進行了幾次嘗試但是我得到了404頁,要么參數不會被$ _GET ['tag_nome']或$ _GET ['id_frase']接收。
我有的實際htaccess是這個,但它不起作用:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.nfrases.com$
RewriteRule ^(.*)$ http://www.nfrases.com/$1 [R=301]
RewriteRule ^([0-9]+)(.*) index.php?id_frase=$1
RewriteRule ^(.*)/([0-9]+) tag.php?tag_nome=$1&id_frase=$2 [L,QSA]
你們能幫助我嗎?
再次感謝您的一切! 我感謝您的幫助。
以下重寫規則適用於RewriteRule測試器 :
#removes trailing slash
#/vida/222/ becomes /vida/222
#/222/ becomes /222
RewriteRule ^(.+)/$ /$1 [R=301]
#/222 silently rewrites to /index.php?id_frase=222
RewriteRule ^([0-9]+)$ index.php?id_frase=$1 [L]
#/vida silently rewrites to /index.php?tag_nome=vida
RewriteRule ^([a-zA-Z-]+)$ index.php?tag_nome=$1 [L]
#/vida/222 silently rewrites to /index.php?tag_nome-vida&id_frase=222
RewriteRule ^([^/]+)/([0-9]+)$ tag.php?tag_nome=$1&id_frase=$2 [L]
你可能想要這樣的東西:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
在這種情況下,nfrases.com / 2222將重新命名為index.php / 2222。 這很容易在index.php文件中允許基本路由。 對於更高級的路由,使用好的框架可能更快更容易。 看看FuelPHP(http:www.fuelphp.com)或CodeIgniter(http://www.codeigniter.com)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.