PHP 重写规则 [英] PHP Rewrite Rules

查看:37
本文介绍了PHP 重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用使用的实际 URL 是:

The actually URL which my app uses is:

http://site.com/search.php?search=iPhone 

但我希望可以用

http://site.com/iPhone

我没有重写规则的经验,我该如何设置?

I have no experience of rewrite rules, how can I set this up?

解决方案已经奏效,但地址栏中会显示新的 URL.我认为可以进行设置,使其看起来好像页面位置是

The solution has worked but the new URL is displayed in the address bar. I thought it would have been possible to set this up so that it appears as though the page location is

http://site.com/iPhone

不改变显示

http://site.com/search.php?search=iPhone 

这可能吗?谢谢.

推荐答案

在您网站的根目录中创建一个名为 .htaccess 的文件并将其放入其中.

Create a file called .htaccess in the root of your website and put this in it.

RewriteEngine on 
Options +FollowSymlinks
RewriteBase / 

RewriteRule ^(.*) search.php?search=$1 [R]

应该可以.

不过,我建议您将其设置得更具体一些,因此可能需要用户在您的 url 中使用搜索目录.例如,而不是 mysite.com/IPhone 使用 mysite.com/search/IPhone 就像

I would suggest however that you make it a bit more specific, so maybe require the user of a search directory in your url. eg instead of mysite.com/IPhone use mysite.com/search/IPhone which would work like

RewriteEngine on 
Options +FollowSymlinks
RewriteBase / 

RewriteRule ^search/(.*) search.php?search=$1 [R]

这样可以更轻松地重定向正常页面,例如关于我们或基本主页.

This makes it easier to have normal pages that arnt redirected, such as about us or a basic homepage.

正如 Chris 所说,这不是 PHP 而是 Apache 执行此操作,它是否有效取决于您的托管设置.

As Chris says, this is not PHP but Apache that does this, and whether it works can depend on your hosting setup.

这篇关于PHP 重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆