I want to rewrite my URL, www.example.com/(language)/contact
into www.example.com/con.htm?lang=(language)
, so what should I use?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
First of all, check that you, on the distant server, have access to a .htaccess
file in the root directory of the server.
Now, you first need to type
RewriteEngine On
in order for the rewrite to work.
Now, you can use the magical RewriteRule
token. What is it? A rewrite rule.
Now the way it works is pretty simple: The URL typed in is to be typed just next to RewriteRule
, with (.*)
representing your variable, here (language)
. The output URL which is the URL to be crawled by the server is just after, and the content of the variable (.*)
we talked about above will be placed where $1
is.
Giving us:
RewriteEngine On
RewriteRule /(.*)/contact /con.htm?lang=$1
as the content of .htaccess
for what you asked.
Input:
www.example.com/fr/contact
Output:
www.example.com/con.htm?lang=fr
And it works everytime!
Also you can add as many RewriteRule
s as you want!