I want to rewrite an URL on my site… What can I do

2019-09-03 09:32发布

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条回答
Explosion°爆炸
2楼-- · 2019-09-03 09:45

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 RewriteRules as you want!

查看更多
登录 后发表回答