简体   繁体   English

重定向htaccess还是php?

[英]redirect htaccess or php?

How do you do this... 你怎么做到这一点...

When the user enters 当用户进入时

http://domain.com/mycompanyname http://domain.com/mycompanyname

browser redirects to 浏览器重定向到

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

Note: mycompanyname value is dynamic 注意:mycompanyname值是动态的

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

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. 尝试这个解决方案(从我对类似问题的回答中复制)除了使用mod_rewrite之外,正如已经报道的那样,你可以用一个简单的技巧做一些魔术。

Put into the .htaccess a directive like this one 将.htaccess这样的指令放入.htaccess中

<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) 用您选择的正则表达式替换^ servlet $(它将是您的调度程序的名称)

The file servlet should be similar to this 文件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
  }
?>

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. 网址可以具有以下形式: http:// yoursite / servlet / reports / sales / 2009您还可以使用.htacces和调度程序获取http:// yoursite / reports / sales / 2009 plaiyng表格。

This method has the advantage that mod_rewrite is not required as FilesMatch (1.3+) and ForceType (2.0+) are in the apache core 此方法的优点是不需要mod_rewrite,因为FilesMatch(1.3+)和ForceType(2.0+)位于apache核心中

See for reference 参见参考
http://httpd.apache.org/docs/2.2/mod/core.html#forcetype http://httpd.apache.org/docs/2.2/mod/core.html#forcetype
http://httpd.apache.org/docs/2.2/mod/core.html#filesmatch 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/ http://www.devarticles.com/c/a/Apache/Using-ForceType-For-Nicer-Page-URLs/1/

place the following code in your http://domain.com/mycompanyname 将以下代码放在您的http://domain.com/mycompanyname

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

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 你可以在php页面的顶部使用它一旦用户访问你的页面,用户就会被重定向到一个新的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 在这种情况下,用户将被重定向到http://www.facebook.com/people/Wasim-Karani/1158880522

I believe PHP will do the job, so I will tell you about it. 我相信PHP会完成这项工作,所以我会告诉你。 First get the URL of the page then split it to get mycompany and save it in a variable. 首先获取页面的URL,然后将其拆分以获取mycompany并将其保存在变量中。 Now let the PHP loads the new page with the variable ... 现在让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();

?>

this will do it using the PHP ... 这将使用PHP ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM