.htaccess 和 mod_rewrite 帮助

发布于 2024-08-21 15:30:55 字数 155 浏览 2 评论 0原文

使用 mod_rewrite,如何将遵循此模式的 URL:

http://example.com/index.php?id=14

转换为此模式:

http://example.com/ 14/

Using mod_rewrite, how could I turn URLs that follow this pattern:

http://example.com/index.php?id=14

Into this pattern:

http://example.com/14/

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

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

发布评论

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

评论(3

归属感 2024-08-28 15:30:55

因为我认为您宁愿将 // 的请求内部重写为 /index.php?id=,请尝试以下操作:

RewriteRule ^(\d+)/$ index.php?id=$1

但是如果您确实希望将请求重定向到 /index.php?id= 内部到 //

RewriteCond %{QUERY_STRING} (^|&)id=(\d+)(&|$)
RewriteRule ^index\.php$ /%2/? [R]

如果您想保留可能的其他 URL论点:

RewriteCond %{QUERY_STRING} ^(([^&]*&+)*)id=(\d+)(&+(.*))?$
RewriteRule ^index\.php$ /%3/?%2%5 [R]

As I think that you rather want to rewrite requests to /<ID>/ internally to /index.php?id=<ID>, try this:

RewriteRule ^(\d+)/$ index.php?id=$1

But if you really want to redirect requests to /index.php?id=<ID> internally to /<ID>/:

RewriteCond %{QUERY_STRING} (^|&)id=(\d+)(&|$)
RewriteRule ^index\.php$ /%2/? [R]

And if you want to preserve possible other URL arguments:

RewriteCond %{QUERY_STRING} ^(([^&]*&+)*)id=(\d+)(&+(.*))?$
RewriteRule ^index\.php$ /%3/?%2%5 [R]
奈何桥上唱咆哮 2024-08-28 15:30:55
RewriteCond %{QUERY_STRING} id=(.+)
RewriteRule ^/index.php$ /%1/? [R]
RewriteCond %{QUERY_STRING} id=(.+)
RewriteRule ^/index.php$ /%1/? [R]
酷炫老祖宗 2024-08-28 15:30:55

您的代码是我的 .htaccess 文件中最终起作用的唯一代码,但是我有第二个查询字符串,但无法使其工作。我的密钥是 id 和 name。我如何将

http://example.com/studio_details.php?id=123&name=Studio Wrox London

转到

http://example.com/123/Studio Wrox London< /strong>
(特别是在这里,我可能会遇到问题,因为数据库中的名称中有空格 - 这可以用 - 解决,还是可以很好地处理空格?)

  1. 以下是我在 .htaccess 文件中的内容来自服务提供商的共享托管服务器。如何向以下内容添加更多查询字符串?

RewriteCond %{QUERY_STRING} ^(([^&]&+))id=(\d+)(&+(.*))?$
RewriteRule ^studio_details.php$ /%3/?%2%5 [R]

  1. 如何将网站中存在的所有 .php 扩展名重写为 .html?因为在我的情况下,所有重写都不起作用。 (仅在第 1 点中)

  2. 尽管我的 .htaccess 文件中没有任何代码,但我将我的 index.php 文件重定向到主机目录,例如“vhost\websites\syildiz\www...”等。有任何规则可以确保索引页面是index.php(将被重写为index.html)

  3. 是否有规则在执行之前检查规则是否有效?比如在显示页面之前检查文件和查询字符串变量是否执行并产生结果?

谢谢
苏尔斯

Your code is the only one worked finally in my .htaccess file, however I have the second querystring and couldnt get it working. My keys are id and name. How can i get

http://example.com/studio_details.php?id=123&name=Studio Wrox London

to

http://example.com/123/Studio Wrox London
(here in particular I might have a problem as there are spaces in the names from the database - can this be resolved with a - or get it working with space fine?)

  1. The below is what I have in the .htaccess file which is on a shared hosting server from a service provider. How can i add more querystrings to the following?

RewriteCond %{QUERY_STRING} ^(([^&]&+))id=(\d+)(&+(.*))?$
RewriteRule ^studio_details.php$ /%3/?%2%5 [R]

  1. How can I rewrite all .php extensions that exist in my website to .html? As none of the rewrites worked in mycase. (only above in point 1)

  2. Eventhough I dont have any code in my .htaccess file I get my index.php file redirected to the host directory such as "vhost\websites\syildiz\www..." etc. Is there any rule for making sure there the index page is index.php (which will be rewritten to index.html)

  3. is there a rule to check if the rule works before executing it? like checking if the file and the querystring variables executed and resulted before displaying the page?

thanks
Suls

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