2016-03-01 150 views
1

嗯,我得到這個代碼在我的.htaccess的.htaccess重寫規則自動URL

RewriteRule page/(.*) ?page=$1 
RewriteRule search/(.*) search.php?search=$1 
RewriteRule post/(.*) post.php?id_post=$1 

和它的作品,但我需要手動更改URL。

而我有很多頁面來改變它。

Theres以任何其他方式將代碼放在.htaccess並使瀏覽器自動獲取正確的URL?

例子:

http://example.com/post.php?id_post=1

http://example.com/post/1

+0

我該怎麼辦?你能告訴我嗎?在先進的 – luckr

回答

2
# Redirect old Page URLs to new URLs 
# Old URL format: http://example.com/?page=pagename 
# New URL format: http://example.com/page/pagename 
RewriteCond %{QUERY_STRING} ^page=(.*)$ 
RewriteRule ^(.*)$ page/%1? [R=301,L] 

# Redirect old Search URLs to new URLs 
# Old URL format: http://example.com/search.php?search=keyword 
# New URL format: http://example.com/search/keyword 
RewriteCond %{REQUEST_URI} search.php 
RewriteCond %{QUERY_STRING} ^search=(.*)$ 
RewriteRule ^(.*)$ search/%1? [R=301,L] 

# Redirect old Post URLs to new URLs 
# Old URL format: http://example.com/post.php?id_post=1 
# New URL format: http://example.com/post/1 
RewriteCond %{REQUEST_URI} post.php 
RewriteCond %{QUERY_STRING} ^id_post=([0-9]*)$ 
RewriteRule ^(.*)$ post/%1? [R=301,L] 

# ** Any requests with an old URL should have been processed before this comment, 
# ** and any requests with a new URL should only be processed by these rules: 

# Support new SEO-friendly URLs 
RewriteRule page/(.*) index.php?page=$1 
RewriteRule search/(.*) search.php?search=$1 
RewriteRule post/(.*) post.php?id_post=$1 
+0

感謝我想它的工作URL更改,但在我的鉻ERR_TOO_MANY_REDIRECTS出現這個錯誤,所以我想錯了什麼可以幫助你,謝謝; D – luckr

+0

我刪除我的htaccess上的一切,並再次測試,仍然有這個問題 – luckr

+0

錯誤表明重定向卡在一個循環中。我修改了我的答案中的代碼,從底部的第三行開始,它有'?page = $ 1'我已將此更改爲'index.php?page = $ 1',看看這是否有幫助。此外,我還在中間添加了一條評論,以幫助解釋處理在舊網址和新網址之間的分佈情況。另外值得注意的是,您可能需要關閉瀏覽器或刷新其緩存,因爲它可能會記住以前的重定向標頭,而不是遵守新規則。 – richhallstoke