htaccess Rewrite 301 with query string (parameters)

Source: Internet
Author: User

The overall URL has changed (the domain name has not changed), because it is the lamp environment, so you want to use htccess to do 301 redirects. Look up the internet, there are roughly two kinds of writing:

The code is as follows Copy Code

1. #第一种
Redirect Permanent a.php b.php
#或
Redirect a.php b.php
#第二种

Rewriterule a.php b.php [l,r=301]

Rewriterule square brackets for modifiers, detailed flag please see here Http://httpd.apache.org/docs/2.4/rewrite/flags.html#page-header

My demand is to put view.php?iid=123 301 to/item/123.

How do you redirect a link with a query character? I tried the following two types but not available:

The code is as follows Copy Code


1.Redirect ^view.php?iid= (d+) $/item/$1
2.RewriteRule ^view.php?iid= (d+) $/item/$1 [l,r=301]


Do htaccess rewrite rules, which 301 jump, found that the URL after the jump automatically added parameter transfer, that is, querystring.

such as the rules

The code is as follows Copy Code

Rewriterule ^brot.* http://local.abc.com [r=301,l]


When this URL is encountered, the parameter is automatically taken

The code is as follows Copy Code

Http://local.abc.com/brot/dsf?abc=1


will become such URLs:

The code is as follows Copy Code
Http://local.abc.com/?abc=1


It was later found that query strings and files were required to be parsed separately, and the final wording was:

The code is as follows Copy Code

1.RewriteCond%{query_string} ^iid= (d+) $
Rewriterule ^view.php$/item/%1? [l,r=301]

#如果你的apache版本 >=2.4, you can use QSD flag

#RewriteRule ^view.php$/item/%1 [L,R=301,QSD]

QSD = qsdiscard Delete query string (new after Apache 2.4)
QSA = qsappend Add Query string (This is more commonly used)


Attention matters

1. Query string use% to do reverse reference, the use of regular expressions and rewriterule is the same,%1 represents the first match except the original string, that is,%0 is iid=123,%1 123
2./item/%1 in the second article? I added a question mark later, because my Apache version is 2.2, only in 2.4 flag [QSD], add an empty query, you can get the results of the final item/123. If you don't use QSD or add "in the back"? "You will get the result is/item/123?iid=123, because 301 by default the query character is added to the


The following is a full set of Learning Diary blog redirection program. For your own memo and need friends for reference .

1. Under the root directory "/":

The code is as follows Copy Code


# BEGIN WordPress


Rewriteengine on

Rewritebase/


#把ab. CN URLs are all redirected to www.ab.cn

Rewritecond% ^ab.cn [NC]

Rewriterule ^ (. *) $ http://www.ab.cn/$1 [l,r=301]


#除开 *.do form of the URL (must), the other is WordPress added to achieve Dynamic Web site pseudo static, the implementation of the principle does not understand

Rewritecond%!-f

Rewritecond%!-d

Rewritecond%!. . Do

Rewriterule. /index. PHP [L]


#把 *.do the URL with the query string to redirect the current URL

Rewritecond% Dis (goal| Diary) contentaction.do

Rewritecond% ^ (([a-za-z]) = ([0-9a-z]*) &) * Goalid= ([0-9]) ((& ([a-za-z]) = ([0-9a-z]*)] *) $

Rewriterule ^dis (goal| Diary) contentaction.do$ http://www.ab.cn/archives/diaries/%5.htm? [r=301]


Rewritecond% rssaction.do

Rewritecond% ^type=latestdiaries (.) $

Rewriterule ^rssaction.do$ http://www.ab.cn/feed? [r=301]


Rewritecond% rssaction.do

Rewritecond% ^type=latestadvices (.) $

Rewriterule ^rssaction.do$ http://www.ab.cn/comments/feed? [r=301]


# End WordPress


2. Under the/archives/goals/directory, redirect the following URL to the/archives/diaries/below:

The code is as follows Copy Code

# control the page under/archives/goals/301 redirect to/archives/diaries/

# this. htaccess is puted into/archives/goals/


Rewriteengine on

Rewritebase/

Rewritecond%!-f

Rewritecond%!-d

Rewriterule ^ (. *) $ http://www.ab.cn/archives/diaries/$1 [r=301]


# End

# 2007.07.14 10:40


3, in the directory/diaries/, the/diaries/103.jsp form of the URL redirect to/archives/diaries/103.htm

The code is as follows Copy Code

# control the page like/diaries/103.jsp redirect To/archives/diaries/103.htm

# this. htaccess is puted into/diaries/


Rewriteengine on

Rewritebase/

Rewritecond%!-f

Rewritecond%!-d

Rewriterule ^ ([0-9]). jsp$ http://www.ab.cn/archives/diaries/$1.htm [r=301]


# End

# 2007.07.14 11:34


4, in the directory/goals/, the/goals/1.jsp form of the URL redirect to/archives/diaries/1.htm

The code is as follows Copy Code

# control the page like/goals/1.jsp redirect To/archives/diaries/1.htm

# this. htaccess is puted into/goals/


Rewriteengine on

Rewritebase/

Rewritecond%!-f

Rewritecond%!-d

Rewriterule ^ ([0-9]). jsp$ http://www.ab.cn/archives/diaries/$1.htm [r=301]


# End

# 2007.07.14 11:45


5, placed in the/java directory, the domain name java.ab.cn or www.java.ab.cn all URLs under the redirect to Www.ab.cn

The code is as follows Copy Code

Rewriteengine on

Rewritecond% ^java.ab.cn$ [OR]

Rewritecond% ^www.java.ab.cn$

Rewriterule ^ (. *) $ http://www.ab.cn/$1 [r=301,l]


6, in the directory/sitemaps/, the/sitemaps/goal1-1. JSP form URL Redirect to/archives/sitemaps/goal1-1.htm

  code is as follows copy code

 

# Control the page like/sitemaps/goal1-1. JSp redirect to/archives/sitemaps/goal1-1.htm

# this. htaccess is puted into/sitemaps/


Rewriteeng ine on

Rewritebase/

Rewritecond%!-f

Rewritecond%!-d

Rewriterule ^ (goal[0-9]-1). jsp$ http://www.ab.cn/archives/sitemaps/$1.htm [r=301]

Rewriterule ^ (goals-1). jsp$ http://www.ab.cn/archives/sitemaps/$1.htm [r=301]


# end

# 2007.07.14 12:00


 

Among them, with the query string redirection is not easy to find, took me a lot of days, asked three forums, looked up n more than the Web page has no results. A friend also made a conclusion: "Unless you use the program, it is impossible to achieve this demand." Finally, search on google.cn with "Rewritecond%" for the keyword before the module mod_rewrite:rewriting URLs with Query strings and mod rewrite query string Problem found a clue, spent an afternoon to debug before successful. It's not easy.


I am not familiar with the mod_rewrite of Apache, some of the above content is also copied from others, the truth is not very clear, perhaps there are errors. Tips for friends who want to find mistakes and need improvement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.