.htaccess RewriteRule自动网址

时间:2016-03-01 10:21:19

标签: php .htaccess mod-rewrite url-rewriting

我在.htaccess

获得了此代码
RewriteRule page/(.*) ?page=$1
RewriteRule search/(.*) search.php?search=$1
RewriteRule post/(.*) post.php?id_post=$1

它有效,但我需要手动更改网址。

我有很多页面可以改变它。

还有其他任何方法可以将代码放在.htaccess并让浏览器自动获取正确的网址吗?

示例:

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

http://example.com/post/1

1 个答案:

答案 0 :(得分: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