简体   繁体   English

url rewrite .htaccess:用相同规则重写2页

[英]url rewrite .htaccess : rewrite 2 pages with same rule

so , i want your help I'm trying to rewrite 2 pages in that way 所以,我想在您的帮助下,尝试以这种方式重写2页

www.mydomain.com/page1.php?id=52

to

www.mydomain.com/52/

and another page2.php to much like this www.mydomain.com/page2/id 和另一个page2.php就像这样www.mydomain.com/page2/id

I did so many research and i found this rule 我做了很多研究,发现了这个规则

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ page1.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?id=$1

but when i tested this rule if i worte in the url www.mydomain.com/page2/id it play nice but i can't get the id variable throw php ( $_GET ) if i remove page2 from the url ( www.mydomain.com/id ) it gave me page1 但是当我测试此规则时,如果我在网址www.mydomain.com/page2/id它,它的播放效果很好,但是如果我从网址( www.mydomain.com/id )中删除page2,则我无法获得ID变量引发php( $_GETwww.mydomain.com/id )它给了我page1

so, is it possible to do that ? 因此,有可能这样做吗?

so, is it possible to do that ? 因此,有可能这样做吗?

Your patterns look exactly the same except there is a trailing slash with one and no trailing slash with the other. 您的模式看起来完全相同,除了一个斜杠带有一个斜杠,而另一个斜杠没有一个斜杠。

So whatever this "id" is, it matches the first rule, ^([a-zA-Z0-9_-]+)$ if it doesn't have a trailing slash, but matches the second rule, ^([a-zA-Z0-9_-]+)/$ if it does have a trailing slash. 因此,无论此“ id”是什么,如果没有尾部斜杠,它都会与第一条规则^([a-zA-Z0-9_-]+)$匹配,但与第二条规则^([a-zA-Z0-9_-]+)/$如果有斜杠)。 That means: 这意味着:

www.mydomain.com/52/

rewrites to: users.php?id=52 , and 重写为: users.php?id=52 ,以及

www.mydomain.com/52

rewrites to: page1.php?id=52 . 重写为: page1.php?id=52

Now, if you want to make it so it works for both with or without the trailing slash, it's impossible. 现在,如果您要使其在有或没有尾斜杠的情况下都适用,那就不可能了。 There's no difference between what 52 matches. 52个匹配项之间没有区别。 For example, if you have this URL: 例如,如果您具有以下URL:

www.mydomain.com/1234

Is that for page1.php ? 那是page1.php吗? or users.php ? users.php吗? Both patterns are exactly the same, thus the first rule always matches and the second rule is always ignored. 两种模式完全相同,因此第一个规则始终匹配,而第二个规则始终被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM