Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

apache - htaccess redirect url with parameters

I have hundreds of links like this:

http://www.domain.com/index.php?tag=value

I want to redirect all links to

http://www.domain.com/value/

Example:

Link1 http://www.domain.com/index.php?tag=LW1fdX49tR redirect to: http://www.domain.com/LW1fdX49tR/

Link2 http://www.domain.com/index.php?tag=A3kh0QLIrc redirect to: http://www.domain.com/A3kh0QLIrc/

Link3 http://www.domain.com/index.php?tag=vXwNR4U9qY redirect to: http://www.domain.com/vXwNR4U9qY/

etc

How can I do that? Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Besides redirecting the request, you probably want to make sure the new url actually works too. You'll need both an external redirect and an internal rewrite for that. In the example below I use the THE_REQUEST trick to only trigger the rule if it is the actual request url, not if it is rewritten internally. It is required to prevent an infinite loop.

#External redirect with THE_REQUEST trick; change R to R=301 when everything works correctly
RewriteCond %{THE_REQUEST} ^(GET|POST) /index.php?tag=(.*) HTTP
RewriteRule ^ /%2? [R,L]

#Internal rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?tag=$1 [L]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...