使用 mod_rewrite 创建 RESTful URL 格式

发布于 2024-09-14 19:00:27 字数 341 浏览 4 评论 0原文

我有一个网页 http://mydomain.com/form.php?id=123 , 我需要保留旧功能的这种格式,并实现新功能,以便用户可以像 http://mydomain.com/123 一样访问此页面。

  1. 因此客户端将看到这个http://mydomain.com/123
  2. 服务器将理解为旧版本http://mydomain.com/form.php?id=123

这样我就不必改变旧的功能。 我知道这可以通过 HTaccess 规则来完成。但不知道怎么做。 请帮忙。谢谢。

I have a webpage http://mydomain.com/form.php?id=123 ,
I need to keep this format for old functionality , and to implement a new functionality so the user could access this page like this http://mydomain.com/123.

  1. So the client will see this http://mydomain.com/123.
  2. And the server will understand as old version http://mydomain.com/form.php?id=123.

So this way I don't have to change the old functionality.
I know that this can be accomplished through HTaccess rule. but don't know how.
Please help. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生再见 2024-09-21 19:00:27

这可能应该为您处理

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ form.php?q=$1 [L,QSA]

!-f排除任何已经存在的文件(因此直接点击/form.php不会被重写),!-d排除任何目录存在(因此,如果您的网站上有真正的 /123/ ,它将不会被重写),否则将其他所有内容重定向到您的 form.php 脚本。

如果您只想重定向纯数字 URL,那么上面的 mhitza 评论就是您想要的。

This should probably handle it for you

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ form.php?q=$1 [L,QSA]

!-f excludes any files which already exist (so hitting /form.php directly will not get rewritten), !-d excludes any directories which exist (so if you have a real /123/ on your site it won't get rewritten), and otherwise redirects everything else to your form.php script.

If you only want to redirect purely numeric URLS, then mhitza's comment above is what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文