= $ 1在URL重写中是什么意思? [英] What does the =$1 mean in url rewriting?

查看:165
本文介绍了= $ 1在URL重写中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在stackoverflow或google上找不到有关=$1含义的任何信息.我得到的只是肤浅的信息,但对于像我这样的初学者却一无所获.它是做什么的?

I can't find any information on stackoverflow or google about the meaning of =$1. I get superficial information but nothing for beginners like me. What does it do?

如果我有这样的东西:

www.website.com/profile.php?simon

simon名称是否对应于$1变量和为什么1 ?

Does the name simon correspond to the $1 variable and why 1?

这是我的理解方式:

  • (.*) profile/profile.php? id = $ 1

粗体对应于:

  • www.website.com/profile.php? id = simon

经重写后转换为:

  • www.website.com/profile/simon

我在这里想念东西吗?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME}.php -d
RewriteRule ^(.*)$ /profile/index.php?id=$1

进行此更改

localhost/test/index.php?philip 到: 本地主机/测试/配置文件/菲利普

localhost/test/index.php?philip to: localhost/test/profile/philip

我试图输入网址,但失败了.我了解regex的作用,但以某种方式使替代品的工作方式完全混乱.

I tried to enter the url but it failed. I understand what regex does but somehow im utterly confusing how the replacement works.

推荐答案

让我尝试用外行的术语进行解释.

Let me try to explain in layman's terms.

假设您通常会链接到这样的页面...

Let's say you would normally link to a page like this...

/listing.php?id=2146_east_fifth_street

/listing.php?id=2146_east_fifth_street

然后您创建一个这样的重写规则...

Then you create a rewrite rule like this...

RewriteRule ^([A-Za-z0-9_-]+)$ listing.php?id=$1 [NC,L]

这部分^([A-Za-z0-9_-]+)$说接受任何带有大写字母/小写字母/0-9/下划线/连字符的querystring参数

This part ^([A-Za-z0-9_-]+)$ says to accept any querystring parameter with uppercase letters / lowercase letters / 0-9 / underscores / hyphens

这部分listing.php?id=$1表示将向浏览器提供什么页面. $1询问第一个querystring参数,并将其附加到URL,如下所示:your-domain.com/2146_east_fifth_street

This part listing.php?id=$1 says what page will be served up to the browser. the $1 asks for the first querystring parameter and appends it to the URL like this... your-domain.com/2146_east_fifth_street

这就是您在网址栏中看到的内容,而不是... your-domain.com/listing.php?id=2146_east_fifth_street

That's what you see in the URL bar instead of... your-domain.com/listing.php?id=2146_east_fifth_street

编辑

重写规则的第二部分是真实"页面所在的位置.

The second part of the rewrite rule is where the "real" page is located.

如果您希望自己的网址显示为/profile/philip

If you want your url to read /profile/philip

您的重写规则将以/profile/开头,像这样...

Your rewrite rule would start with /profile/ like this...

RewriteRule ^profile/(.*)$ path/to/the/real/file/index.php?id=$1

这篇关于= $ 1在URL重写中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆