2013-10-23 64 views
0

我很新手在修改.htaccess和我需要一些幫助,使其工作。使用.htaccess自動URL重寫

例如,我有這兩個網址:

  • www.example.com/categories/example1
  • www.example.com/categories/example2

,我想自動把它改寫爲:

  • www.example.com/mypage/?cat=example1
  • www.example.com/mypage/?cat=example2

有沒有辦法用.htaccess重寫?

我已經嘗試過這一點,但仍然沒有工作...

RewriteEngine On 
RewriteBase/
RewriteRule ^example1([^/]*)$ mypage/?cat=$1 [L] 
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /mypage/ 
RewriteCond %{QUERY_STRING} name=([^\&]*) 
RewriteRule ^mypage/$ categories/%1.php? [R,L] 

回答

0

你想要的「example1」位爲「categories」你不想重定向與「.PHP」結束。所以像這樣:

RewriteEngine On 
RewriteBase/

RewriteRule ^categories/([^/]*)$ mypage/?cat=$1 [L,QSA] 

RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /mypage/ 
RewriteCond %{QUERY_STRING} cat=([^\&]*) 
RewriteRule ^mypage/$ categories/%1? [R,L] 
+0

它不工作。這是因爲我使用wordpress,我必須在代碼中添加其他內容? –

+0

@JohnSmith如果你使用wordpress,那麼使用WordPress的內置重寫永久鏈接 –

+0

但我不能使用.htaccess做到這一點? –