如何在php中重写url? [英] How to rewrite url in php?

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

问题描述

关于在php中重写网址,我有一些问题要问.

I have some question to ask about rewrite url in php.

1- www.test.com/index.php?name=123到www.test.com/123

1- www.test.com/index.php?name=123 to www.test.com/123

2- www.test.com/folder1/index.php?name=123到www.test.com/folder1/123/index.php

2- www.test.com/folder1/index.php?name=123 to www.test.com/folder1/123/index.php

关于我在数字1上的编码,但是我不知道如何用数字1重写数字2的组合.

Hereabout my coding on number 1 but i don't know how to rewrite number 2 combination with number 1:

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

推荐答案

当您说用php重写url"时,我认为用php替代"是指应用程序的语言,而不是重写/重定向方法---因为您的示例代码由Apache指令组成.

以下规则会将test.com/folder1/index.php?name=123形式的URL更改为test.com/folder1/123形式的漂亮" URL(我删除了"index.php"变得更加人性化).

The rules below will change URLs in the form test.com/folder1/index.php?name=123 into a "pretty" URL of the form test.com/folder1/123 (I removed the 'index.php' to be even more user-friendly).

  1. 重定向丑陋的查询URL,以便用户始终看到漂亮" URL

  1. Redirect the ugly query URL so users always see the "pretty" URL

RewriteCond  %{QUERY_STRING} \bname=([^&]+)
RewriteRule  ^/?folder1/index\.php$ /folder1/%1 [R=302]

  • 告诉服务器如何解释漂亮的URL

  • Tell the server how to interpret the pretty URL

    RewriteRule  ^/?folder1/([^\/]+)(/index\.php)?$ /folder1/index.php?name=$1 [L]
    

  • 注意: 上面的第一个规则使用302(临时)重定向,因此在测试和调整重定向规则时,浏览器不会记住"重定向规则.如果您关心SEO,请将[R=302]更改为[R=301],这是永久重定向.

    NOTE: The first rule above uses a 302 (temporary) redirect, so your browser doesn't "memorize" the redirect rule while you're testing and tweaking them. If you care about SEO, change the [R=302] to [R=301], which is a permanent redirect.

    这篇关于如何在php中重写url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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