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

南笙酒味 提交于 2019-12-12 00:18:02

问题


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!



来源:https://stackoverflow.com/questions/33297530/i-want-to-rewrite-an-url-on-my-site-what-can-i-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!