Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在使用htaccess重写动态url时生成href_Php_.htaccess_Url Rewriting_Href - Fatal编程技术网

Php 如何在使用htaccess重写动态url时生成href

Php 如何在使用htaccess重写动态url时生成href,php,.htaccess,url-rewriting,href,Php,.htaccess,Url Rewriting,Href,我想使用.htaccess文件创建友好的URL。但是当我点击href中指定的链接时 <a href="category.php?id= <?php echo "{$row['name']}"; ?>">Example </a> 这只是将传递的参数存储在$result变量中,并将其显示在屏幕上 代码(.HTACCESS文件): 当我点击链接时,它会显示像http://mydomain.com/category.php?id=Rohit 而不是http://my

我想使用.htaccess文件创建友好的URL。但是当我点击href中指定的链接时

<a href="category.php?id= <?php echo "{$row['name']}"; ?>">Example </a>
这只是将传递的参数存储在
$result
变量中,并将其显示在屏幕上

代码(.HTACCESS文件):

当我点击链接时,它会显示像
http://mydomain.com/category.php?id=Rohit
而不是
http://mydomain.com/website/rohit

但是,当我将URL手动写入
http://mydomain.com/website/rohit
它显示相同的页面。这表示.htaccess文件正在工作。我想在单击word示例时直接显示上面的URL

一个例子
当我们点击维基百科中的巴拉克·奥巴马页面时,它会进入
http://en.wikipedia.org/wiki/Barack_obama
但原始链接也可以使用-
http://en.wikipedia.org/w/index.php?title=Barack_obama
。两个页面显示相同的数据。 它们不会向用户显示第二个URL,而是另一个。我想达到同样的结果

如何编写链接以使其能够重写URL?

更改

<a href="category.php?id= <?php echo "{$row['name']}"; ?>">Example </a>


更改

<a href="category.php?id= <?php echo "{$row['name']}"; ?>">Example </a>


使用apache mode\u rewrite和mod\u replace可以做到这一点,但我不推荐这样做。因为我猜您希望在这两种方式上都支持它,并且您正在使用PHP,所以我将创建一个函数(或类/方法)来重写您编写的URL。 i、 e:


通过这种方式,可以根据用户请求处理不同的URL

function getUrl($phpfile, $argument) {

   if ('<some logic to check if user request is rewritten') {    
      return "website/" . $argument; 
   } else {    
      return $phpfile . "?id=" . $argument; 
   }
}
函数getUrl($phpfile,$argument){
如果(可以使用apache mode_rewrite和mod_replace来实现这一点,但我不推荐使用它。因为我猜您希望以两种方式支持它,并且您使用的是PHP,我将创建一个函数(或类/方法)来重写您编写的URL。 i、 e:


通过这种方式,可以根据用户请求处理不同的URL

function getUrl($phpfile, $argument) {

   if ('<some logic to check if user request is rewritten') {    
      return "website/" . $argument; 
   } else {    
      return $phpfile . "?id=" . $argument; 
   }
}
函数getUrl($phpfile,$argument){
如果(“如果您正在使用url重写,那么在href中您必须自己传递SEO友好url;为了获得最佳实践,请创建一个通用函数并将参数传递到该函数中,并为href生成SEO友好url

或者在href hardcode href url like website/123中创建类似category.php?id=123的url


或者您必须定义规则,如用户是否访问您的页面,如category.php?id=123,然后将用户重定向到网站/123

如果您使用url重写,则在href中您必须自己传递SEO友好的url;为了获得最佳实践,请创建一个公共函数,并将参数传递到该函数中,然后生成SEO友好的url f或href

或者在href hardcode href url like website/123中创建类似category.php?id=123的url


或者你必须定义规则,比如如果用户访问你的页面,比如category.php?id=123,然后将用户重定向到网站/123

这种方法更灵活。这种方法更灵活。
<a href="<?php getUrl('category.php', $row['name']}) ?>">Example</a>
function getUrl($phpfile, $argument) {

   if ('<some logic to check if user request is rewritten') {    
      return "website/" . $argument; 
   } else {    
      return $phpfile . "?id=" . $argument; 
   }
}