2012-07-04 46 views
1
RewriteEngine On 
RewriteBase/
# Use the following rule if you want to make the page like a directory 
RewriteRule ^(v)$ /$1/ 
# The following rule does the rewrite. 
RewriteRule ^(.+)/$ profile.php?name=$1 
# The following rewrite the other way round: 
RewriteCond %{REQUEST_URI} ^/profile.php 
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php 
RewriteCond %{QUERY_STRING} name=([^&]+) 
RewriteRule ^profile.php$ %1? 

我在我的.htaccess文件做上面這段代碼,這是因爲之前,我profile.php顯示的是這種URL的工作正常,在我的profile.php頁:的.htaccess多個規則

http://mysite.com/profile.php?name=john 

但是當我寫的代碼是這樣的結果:

http://mysite.com/john/ 

我也想這個代碼適用於我的category.php,所以我所做的只是複製上面的代碼和我的整個的.htaccess文件看起來像這樣:

RewriteEngine On 
RewriteBase/

# Use the following rule if you want to make the page like a directory 
RewriteRule ^(v)$ /$1/ 

# The following rule does the rewrite. 
RewriteRule ^(.+)/$ profile.php?name=$1 
RewriteRule ^(.+)/$ category.php?cid=$1 

# The following rewrite the other way round: 
RewriteCond %{REQUEST_URI} ^/profile.php 
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php 
RewriteCond %{QUERY_STRING} name=([^&]+) 
RewriteRule ^profile.php$ %1? 

RewriteCond %{REQUEST_URI} ^/category.php 
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /category.php 
RewriteCond %{QUERY_STRING} cid=([^&]+) 
RewriteRule ^category.php$ %1? 

但這個代碼給了我一個奇怪的結果,因爲當我訪問 http://mysite.com/john/說我看到的是我的category.php內容的內容。

我應該在這裏做什麼?任何幫助將不勝感激和獎勵!

謝謝!

+0

系統應如何知道什麼是名稱和類別?你需要首先定義。 – jeroen

+0

@jeroen我的類別的URL是這樣的: http://mysite.com/category.php?id=1雖然我的個人資料是這樣的:http://mysite.com/profile.php?id= 1 – PinoyStackOverflower

+0

是的,但是你的改寫的網址是'mysite.com/john /'和'mysite.com/movies /'。什麼在新的url中定義了一個類別以及什麼名字? – jeroen

回答

1

我在你的規則中遇到了問題。我想問題是,你的服務器是無法區分請求類別頁/個人資料頁,每當一個請求到來時一樣 http://mysite.com/john/

所以我建議做任何的個人資料頁面請求或類別頁面請求一個變化。

只需添加http://mysite.com/profile/john/

希望這個作品出來。

0

貌似我解決它使用此一:

RewriteEngine On 
RewriteBase/

# Use the following rule if you want to make the page like a directory 
RewriteRule ^user/(!(profile.php))$ user/$1/ [R=301,L] 
RewriteRule ^category/(!(category.php))$ category/$1/ [R=301,L] 

# The following rule does the rewrite. 
RewriteRule ^user/(.+)/$ profile.php?id=$1 
RewriteRule ^category/(.+)/$ category.php?id=$1 

# The following rewrite the other way round: 
RewriteCond %{REQUEST_URI} ^/profile.php 
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php 
RewriteCond %{QUERY_STRING} id=([^&]+) 
RewriteRule ^profile.php$ user/%1? 

RewriteCond %{REQUEST_URI} ^/category.php 
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /category.php 
RewriteCond %{QUERY_STRING} id=([^&]+) 
RewriteRule ^category.php$ category/%1? 
+0

但是我使用了這段代碼,並且輸入http://mysite.com/user/john(在最後一個字符中沒有斜槓),它返回一個404錯誤,但是如果有的話,它會完美返回。你認爲這裏發生了什麼? – PinoyStackOverflower

1

要回答這個問題,因爲它是在意見修改:

爲用戶和類別之間的區別,如果這些術語出現在URL,您只需要每條規則一行(未經測試的示例):

RewriteEngine On 
RewriteBase/

RewriteRule ^user/(\w+)/?$ /profile.php?name=$1 // using only a limited set of chars (letters and underscore) for the user name and an optional/at the end 

RewriteRule ^category/(\w+)/?$ /category.php?cid=$1 // using only a limited set of chars for the category (letters and underscore) and an optional/at the end 
+0

感謝您的回答,我也在下面做出自己的回答。 :-) – PinoyStackOverflower