网络配置请求
我对 web.config 文件没有太多经验,但我想做的是将所有请求重定向到 index.php
仅添加 ?url=REQUESTED_PAGE
例如
,如果您访问 domain.com/about.php?query=string
它将把 url 重写为 domain.com/about/
给用户 但处理 urldomain.com/index.php?url=about.php&query=string
的
想法是拥有漂亮的用户友好的 url,同时 index.php 计算出显示哪个页面用户。 (我正在使用 smarty,所以要使用哪个模板)
编辑:我正在寻找 web.config 的帮助,而不是 .htaccsess 我仅限于客户端托管,不幸的是,这不是 apache,也是我能找到的唯一可能或可能的 mod不能与 iis7.5 一起使用是 45 英镑,我无法获得资金:(
编辑:我最近的尝试是
<rules>
<rule name="page proc" stopProcessing="true">
<match url="domain.org.uk(.*?)\?(.*)" />
<action type="Redirect" url="{R:0}?url={R:1}?{R:2}" />
</rule>
</rules>
我认为我已经接近但仍然无法工作
已解决::!最终结果!
<rewrite>
<rules>
<rule name="page proc">
<match url="(.*?)\/" />
<action type="Rewrite" url="/index.php?url={R:1}" appendQueryString="false" logRewrittenUrl="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{QUERY_STRING}" pattern="(.*?)(css|js|pdf|jpg|png|gif)" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>
I don't have much experience with web.config files but what I am trying to do is redirect all requests to index.php
only add ?url=REQUESTED_PAGE
eg
if you went to domain.com/about.php?query=string
it would rewrite the url as domain.com/about/
to the user
but process the urldomain.com/index.php?url=about.php&query=string
the idea is to have nice looking user friendly urls whilst the index.php works out which page to show the user. (well am using smarty so which template to use)
EDIT: I am looking for help with web.config not .htaccsess I am restricted to the clients hosting and that isn't apache unfortunately and the only mod I can find that may or may not work with iis7.5 is £45 which I can not get funding for :(
EDIT: My latest attempt is
<rules>
<rule name="page proc" stopProcessing="true">
<match url="domain.org.uk(.*?)\?(.*)" />
<action type="Redirect" url="{R:0}?url={R:1}?{R:2}" />
</rule>
</rules>
I think I am close but still not working
SOLVED :: !END RESULT!
<rewrite>
<rules>
<rule name="page proc">
<match url="(.*?)\/" />
<action type="Rewrite" url="/index.php?url={R:1}" appendQueryString="false" logRewrittenUrl="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{QUERY_STRING}" pattern="(.*?)(css|js|pdf|jpg|png|gif)" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您询问的解决方案:
请告诉我它是否有效。
Here's the solution you asked:
Please tell me if it works.
您需要使用以下内容创建一个
.htaccess
文件:这会将所有请求(除非它是现有文件或目录)传递到您的
index.php
脚本,并且您将能够通过执行以下操作找出用户在 PHP 脚本中请求的内容:因此,您可以在斜杠处拆分传递的
url
参数,并将其传递给正确的控制器或其他内容。您不重写现有文件或目录的原因是静态文件,例如图像、样式表、JavaScript 文件等。否则,对那些确实存在的文件的请求仍将传递到您的
index.php脚本。
此外,如果您希望使用搜索表单和分页等功能(如果您更喜欢使用
$),则
参数也可以代替 URL 段。RewriteRule
之后的QSA
标志允许您继续在 URL 中使用查询字符串。 _GETYou'd need to create a
.htaccess
file with the following:This will pass all requests (unless it's an existing file or directory) to your
index.php
script, and you'll be able to find out what the user requested in your PHP script by doing:So you may split the passed
url
parameter at the slashes and pass it off to the correct controller or whatever.The reason you don't rewrite existing files or directories is static files like images, stylesheets, JavaScript files etc. Otherwise requests for those files that do exist would still be passed to your
index.php
script.Also, the
QSA
flag after theRewriteRule
allows you to continue using query strings in URLs if you wish for things like search form and pagination if you prefer to use$_GET
parameters for those too instead of URL segments.