Recently, a website is being deployed, the server is Apache, and pseudo-static URL rewriting is required, so this article will briefly write this pseudo-static rule, and introduce some various URL rewriting situations by the way.

URL Rewrite Rules

Assuming that the website domain name is www.qq.com the effect I want to achieve is to visit www.qq.com/artcle/12548789954115 to open the article page.

Actually www.qq.com/artcle/index.php?id=12548789954115 is the real access path. We use URL rewriting rules to rewrite the link to www.qq.com/artcle/12548789954115 .

rule writing

 RewriteEngine on
RewriteRule ^artcle/([0-9]*)$ artcle/index.php?id=$1

^ refers to starting from here, artcle refers to a directory starting from here, if it is the root directory, then it is just a slash, ([0-9]*)$ is a regular matching rule, which matches here The thing is that only numbers can be passed in. artcle/index.php?id=$1 is obviously the original real path, where $1 refers to matching the first parameter.

Knowing the meaning of URL rewriting rules is easy. Here are some common URL rewriting rules.

1. www.qq.com/artcle/s.php?id=12345 rewrite as www.qq.com/artcle/12345.html

 RewriteEngine on
RewriteRule ^artcle/([0-9]*).html$ artcle/s.php?id=$1

2. www.qq.com/artcle/s.php?id=12345 is rewritten as www.qq.com/artcle/12345-1-1.html

 RewriteEngine on
RewriteRule ^artcle/([0-9]*)-1-1.html$ artcle/s.php?id=$1

3. www.qq.com/artcle/s.php?id=abcdefg rewritten as www.qq.com/artcle/abcdefg

 RewriteEngine on
RewriteRule ^artcle/(w+)$ artcle/s.php?id=$1

In fact, according to the fact that you understand the writing of the rules, it is easy to DIY your own pseudo-static URL rewriting rules!

author

TANKING

blog

https://www.likeyunba.com

original

https://www.likeyunba.com/artcle/20220818166083464839358


TANKING
4.8k 声望493 粉丝

热爱分享,热爱创作,热爱研究。