简体   繁体   中英

url rewrite .htaccess : rewrite 2 pages with same rule

so , i want your help I'm trying to rewrite 2 pages in that way

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

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

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. That means:

www.mydomain.com/52/

rewrites to: users.php?id=52 , and

www.mydomain.com/52

rewrites to: 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. For example, if you have this URL:

www.mydomain.com/1234

Is that for page1.php ? or users.php ? Both patterns are exactly the same, thus the first rule always matches and the second rule is always ignored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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