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

2019-09-03 09:20发布

问题:

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