重定向 htaccess 还是 php?

发布于 2024-09-18 11:43:19 字数 353 浏览 5 评论 0原文

你怎么做到这一点...

当用户输入

http://domain.com/mycompanyname

浏览器重定向到

http://manager.domain.com/page.php?company=mycompanyname

注意:mycompanyname 值是动态的

How do you do this...

When the user enters

http://domain.com/mycompanyname

browser redirects to

http://manager.domain.com/page.php?company=mycompanyname

Note: mycompanyname value is dynamic

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

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

发布评论

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

评论(5

浊酒尽余欢 2024-09-25 11:43:19
Redirect http://domain.com/mycompanyname http://manager.domain.com/page.php?company=mycompanyname
Redirect http://domain.com/mycompanyname http://manager.domain.com/page.php?company=mycompanyname
眼眸里的快感 2024-09-25 11:43:19

尝试这个解决方案(从我对 类似问题 的回答复制)
除了使用 mod_rewrite 之外,正如已经报道的那样,您可以通过一个简单的技巧来实现一些魔法。

将像这样的指令放入 .htaccess 中,

<FilesMatch "^servlet$"> 
  ForceType application/x-httpd-php
</FilesMatch> 

用您选择的正则表达式替换 ^servlet$ (它将是您的调度程序的名称)

文件 servlet 应该与此类似

<?php
  $data = explode('/',$HTTP_SERVER_VARS['PATH_INFO']); // $data[0] always empty
  $fileToInclude = $data[1].'.php';
  if (file_exists($data[1]) {
     $params=array_slice($data,2); // you can do here something more sophisticated
                                   // for example sanitize parameters or assemble 
                                   // an hash
     include ($fileToInclude);     //Think to this file as a servlet
  } else {
    // issue a 404 error, maybe one of the 500 series
  }
?>

url 可以采用以下形式: http://yoursite/servlet/reports/sales/2009
您还可以访问表单http://yoursite/reports/sales/2009
稍微玩一下 .htacces 和调度程序。

此方法的优点是不需要 mod_rewrite,因为 FilesMatch (1.3+) 和 ForceType (2.0+) 都在 apache 核心中

请参阅参考
http://httpd.apache.org/docs/2.2/mod/ core.html#forcetype
http://httpd.apache.org/docs/2.2/mod/ core.html#filesmatch
http://www.devarticles .com/c/a/Apache/Using-ForceType-For-Nicer-Page-URLs/1/

Try this solution (copied from my answer to similar question )
Other than using mod_rewrite, as already reported you can do a little of magic with a simple trick.

Put into the .htaccess a directive like this one

<FilesMatch "^servlet$"> 
  ForceType application/x-httpd-php
</FilesMatch> 

substitute ^servlet$ with a regular expression of your choice (it will be the name of your dispatcher)

The file servlet should be similar to this

<?php
  $data = explode('/',$HTTP_SERVER_VARS['PATH_INFO']); // $data[0] always empty
  $fileToInclude = $data[1].'.php';
  if (file_exists($data[1]) {
     $params=array_slice($data,2); // you can do here something more sophisticated
                                   // for example sanitize parameters or assemble 
                                   // an hash
     include ($fileToInclude);     //Think to this file as a servlet
  } else {
    // issue a 404 error, maybe one of the 500 series
  }
?>

The url can have the form: http://yoursite/servlet/reports/sales/2009
you can also reach the form http://yoursite/reports/sales/2009
plaiyng a little with the .htacces and the dispatcher.

This method has the advantage that mod_rewrite is not required as FilesMatch (1.3+) and ForceType (2.0+) are in the apache core

See for reference
http://httpd.apache.org/docs/2.2/mod/core.html#forcetype
http://httpd.apache.org/docs/2.2/mod/core.html#filesmatch
http://www.devarticles.com/c/a/Apache/Using-ForceType-For-Nicer-Page-URLs/1/

箹锭⒈辈孓 2024-09-25 11:43:19

将以下代码放入您的 http://domain.com/mycompanyname

<?php
    header('Location: http://manager.domain.com/page.php?company=mycompanyname');
?>

place the following code in your http://domain.com/mycompanyname

<?php
    header('Location: http://manager.domain.com/page.php?company=mycompanyname');
?>
栖迟 2024-09-25 11:43:19

您可以在 php 页面顶部使用它
一旦用户访问您的页面,用户就会被重定向到新的 url

Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.facebook.com/people/Wasim-Karani/1158880522" );

在这种情况下,用户将被重定向到 http://www.facebook.com/people/Wasim-Karani/1158880522

You can use this at the top of your php page
As soon as user visits your page, user would be redirected to a new-url

Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.facebook.com/people/Wasim-Karani/1158880522" );

In this case user will be redirected to http://www.facebook.com/people/Wasim-Karani/1158880522

梦归所梦 2024-09-25 11:43:19

我相信 PHP 可以完成这项工作,所以我会告诉你它。首先获取页面的 URL,然后将其拆分以获取 mycompany 并将其保存在变量中。现在让 PHP 加载带有变量的新页面...

<?php

$URL = ($_SERVER['HTTPS'])=='on'?'https':'http'.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

$companyName = explode('/',$URL);

$URL = 'http://localhost/anything/'.$comapnyName[3];

header('Location: http://localhost/anything/'.$companyName[3]);

exit();

?>

这将使用 PHP 来完成...

I believe PHP will do the job, so I will tell you about it. First get the URL of the page then split it to get mycompany and save it in a variable. Now let the PHP loads the new page with the variable ...

<?php

$URL = ($_SERVER['HTTPS'])=='on'?'https':'http'.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

$companyName = explode('/',$URL);

$URL = 'http://localhost/anything/'.$comapnyName[3];

header('Location: http://localhost/anything/'.$companyName[3]);

exit();

?>

this will do it using the PHP ...

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