modrewrite php 查询字符串

发布于 2024-08-26 03:43:33 字数 378 浏览 2 评论 0原文

我想改变:

http://example.com /index.php?p=blog&pid=2&lid=1&l=en

进入

http://example.com/en/blog.html

或只是

http://example.com/blog.html

I would like to change:

http://example.com/index.php?p=blog&pid=2&lid=1&l=en

into just

http:// example.com/en/blog.html

or just

http:// example.com/blog.html

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

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

发布评论

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

评论(1

¢蛋碎的人ぎ生 2024-09-02 03:43:33

我认为您正在尝试实现更友好的网址,因此我假设您希望在内部将 http://example.com/en/blog.html 重写为http://example.com/index.php?p=blog&pid=2&lid=1&l=en

我推荐的最实用的方法是将所有内容重写到 PHP 脚本中,并在 PHP 内部进行重写。这样您的 PHP 应用程序就可以完全控制 URI。

RewriteEngine On
RewriteBase /

RewriteRule .* index.php [L]

在 PHP 中,您可以通过超全局变量访问原始 URI:$_SERVER[ "REQUEST_URI" ]。注意:一定要消毒,一定要消毒,一定要消毒!


编辑

如果您想通过.htaccess完成整个操作,请参阅下面的示例。但请注意,如果您将来要扩展 URI 结构(即通过添加新规则),那么与在 PHP 应用程序中执行此方法相比,这种方法可能会变得更难以维护。

RewriteEngine On
RewriteBase /

# http://example.com/en/blog.html
RewriteRule ^([^\/]*)/([^\/]*)\.html$ index.php?l=$1&p=$2 [NC,QSA,L]

# http://example.com/blog.html
RewriteRule ^([^\/]*)\.html$ index.php?p=$1 [NC,QSA,L]

# alternatively you can change the last line to add a default language (if not present)
RewriteRule ^([^\/]*)\.html$ index.php?l=sk&p=$1 [NC,QSA,L]

编辑#2:在所有三个规则中添加了缺少的^字符,即更改了([\/]*)([^\/]*)

I think that you are trying to achieve more friendly URLs, so I'm assuming that wan to internally rewrite http://example.com/en/blog.html to http://example.com/index.php?p=blog&pid=2&lid=1&l=en.

The most practical way that I can recommend is to rewrite everything to your PHP script, and do the rewriting internally in PHP. That way your PHP application can have full control over URIs.

RewriteEngine On
RewriteBase /

RewriteRule .* index.php [L]

In PHP, you can then access the original URI via superglobals: $_SERVER[ "REQUEST_URI" ]. Note: sanitize, sanitize, sanitize!


Edit

If you want to do the whole thing via .htaccess, then see the example below. Note however that if you'll expand your URI structure in the future (i.e. by adding new rules), then this approach might become more difficult to maintain as opposed to doing it within your PHP application.

RewriteEngine On
RewriteBase /

# http://example.com/en/blog.html
RewriteRule ^([^\/]*)/([^\/]*)\.html$ index.php?l=$1&p=$2 [NC,QSA,L]

# http://example.com/blog.html
RewriteRule ^([^\/]*)\.html$ index.php?p=$1 [NC,QSA,L]

# alternatively you can change the last line to add a default language (if not present)
RewriteRule ^([^\/]*)\.html$ index.php?l=sk&p=$1 [NC,QSA,L]

Edit #2: added a missing ^ character in all three rules, i.e. changed ([\/]*) to ([^\/]*)

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