2012-12-03 36 views
1

我想讓我的網址看起來像這樣:「http://www.example.com/start」, 而不是這樣:「http://www.example.com /index.php/start」。 與此代碼在我的.htaccess正常工作:.htaccess - Querystring without index.php

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php?/$1 

在一個頁面上我有GET參數工作,URL應該是這樣的: 「http://www.example.com/藝術家/畢加索」, 而不是像這樣: ‘http://www.example.com/index.php/artists?artist=picasso’ 這也有可能用下面的代碼:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/$ /index.php?/$1 
RewriteRule ^artists/(.*)$ /index.php/artists?artist=$1 [L] 

現在,這個解決方案的問題是,所有其他網址現在都必須以斜槓結尾。 所以它看起來是這樣的:「http://www.example.com/start/」,
而不是這樣的:「http://www.example.com/start」

感謝你的幫助 !

回答

1

試着改變你的規則:RewriteRule ^(.*)/$ /index.php?/$1這意味着它有一個斜線結束,以匹配的模式:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !artists/ 
RewriteRule ^(.*?)/?$ /index.php?/$1 
RewriteRule ^artists/(.*)$ /index.php/artists?artist=$1 [L] 

主要是,你在你的模式結束有/$。將其更改爲(.*?)/?$使其成爲可選項。

+0

非常感謝! :)這工作。但是「RewriteCond%{REQUEST_URI}!artists /」是做什麼的? – Andri

+0

@Andri它不是必須的,但它是爲了防止它匹配像'/ artists /'這樣的請求 –