htaccess 重写 url - 如何在代码中写入 url?

发布于 2024-10-06 23:20:55 字数 374 浏览 0 评论 0原文

使用 apache/.htaccess RewriteEngine:

之类的 url 更改

如果我想将www.foo.com/mypage.php?var=abc&var2=123

www.foo.com/abc/123

,我是否需要重写我的 URL我的 PHP 文件是手动处理的,还是也可以处理?

即,更改我的代码

mod_rewrite 可以帮我处理这个问题吗?或者它只翻译收到的 URL,而不是根据 .htaccess 文件中的规则实际重写生成的 HTML?

Using apache/.htaccess RewriteEngine:

If I want to change a url like

www.foo.com/mypage.php?var=abc&var2=123

to

www.foo.com/abc/123

Do I need to rewrite my URLs in my PHP files manually, or is that also handled?

i.e., change in my code <a href="mypage.php?var=abc&var2=123"> to <a href="abc/123">

Does mod_rewrite handle this for me? Or does it only translate the URL it receives, and not actually rewrite generated HTML based on the rules in the .htaccess file?

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

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

发布评论

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

评论(2

思念绕指尖 2024-10-13 23:20:55

如果您有服务器 httpd 配置访问权限,您可以将以下代码放入服务器 apache 服务器配置中或放入 httaccess 文件中

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mypage.php [L]
</IfModule>

,并将以下代码放入 mypage.php 文件顶部,

$Get_data = explode('/', $_SERVER[REQUEST_URI]);
$_GET['var'] = $Get_data[1];
$_GET['var2'] = $Get_data[2];

现在,当您使用这些 www 时,您会得到相同的结果.foo.com/mypage.php?var=abc&var2=123
url 或这些 www.foo.com/abc/123 url。

有什么意见请告诉我。

If you have server httpd configuration access, You can Place the following code in your server apache server config or place into your httaccess file

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mypage.php [L]
</IfModule>

and place the following code in your mypage.php file top,

$Get_data = explode('/', $_SERVER[REQUEST_URI]);
$_GET['var'] = $Get_data[1];
$_GET['var2'] = $Get_data[2];

Now you got same result when you are using these www.foo.com/mypage.php?var=abc&var2=123
url or these www.foo.com/abc/123 url.

Any comments, please let me know.

动次打次papapa 2024-10-13 23:20:55

是的,你必须改变它。

Mod 重写仅在内部重定向请求。它更改了应用程序可见的 HTTP 标头,但您仍然可以通过附加的附加标头字段来重现原始 url。它不会修改您发送的任何内容。

Yes you have to change it.

Mod rewrite only internally redirects requests internally. It changse the HTTP header that is visible to your application, but you will still be able to reproduce the original url by additional header fields that are ardded. It does not modify any content you send.

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