繁体   English   中英

将页面URL更改为PHP

[英]Changing Page URL To PHP

嗨,我想将网页网址(例如www.example.com/pictures.php?title=yyoy转换为类似www.example.com/page/yoyo.php

我怎样才能做到这一点?

www.example.com/pictures.php?title=yyoy这些页面是在有人将图像上传到我的网站时生成的,因此我想将这些类型的URL转换为上面显示的示例。

因为这些页面无法在搜索引擎中建立索引。 谢谢

Picture.php

<?php 
include("connection.php");

if(isset($_GET['title'])){

$page_id = $_GET['title'];

    $select_query = "select * from save_data where Title='$page_id'";

$run_query = mysql_query($select_query);

while($row=mysql_fetch_array($run_query)){

    $post_id = $row['ID']; 
    $post_title = $row['Title'];
    $post_image = $row['Name'];

$page_title = $_GET['title'];
header('uploads/ ' . $page_title . '.php')

?>
<center>
<h2>
<a href="pictures.php?title=<?php echo $post_title; ?>">

<?php echo $post_title; ?>

</a></center>

</h2>

<center><img src="uploads/<?php echo $post_image; ?>"  /></center>


<?php } }?>

我尚未对此进行测试,但是您应该在.htaccess文件中使用类似于以下规则:

RewriteEngine on
RewriteRule   ^pictures.php?title=(.+)$  page/$1.php  [R]

请注意,您需要启用mod_rewrite才能在apache2工作

通常,从表单发布时,我会在中间创建一个页面,但是在您的情况下,您的链接如下所示:

www.example.com/pictures.php?title=yoyo

因此,它会将您定向到在URL中带有变量“ title”的pictures.php。

因此,在您的pictures.php页面上使用:

$page_title = $_GET['title'];

而当您想转到yoyo.php时:

header('Location: $page_title.php')

通过您的编辑,此代码将重定向页面,但不会显示最后的HTML:

<?php 
  include("connection.php");

  if(isset($_GET['title'])){

    $page_id = $_GET['title'];

    $select_query = "select * from save_data where Title='$page_id'";

    $run_query = mysqli_query($select_query);

    while($row=mysqli_fetch_assoc($run_query)){

      $post_id = $row['ID'];       //Just make sure the values between [''] match -
      $post_title = $row['Title']; //your columns in database.
      $post_image = $row['Name'];
    }

    header('Location:uploads/$post_title.php');
?>

这将被忽略:

<center>
  <h2>
  <a href="pictures.php?title=<?php echo $post_title; ?>">

  <?php echo $post_title; ?>

  </a></center>

  </h2>

  <center><img src="uploads/<?php echo $post_image; ?>"  /></center>


  <?php } }?>

这里有一个很好的答案: 在PHP中传递页面时,请屏蔽URL的$ _GET变量,这将告诉您如何屏蔽URL。

或者,您可以将这些文件上载到文件结构中的名为“ page /”的目录中,然后可以使用www.example.com/page/filename.php访问页面目录中的文件(作为下载)。

URE这个

RewriteEngine on
RewriteRule   ^pictures.php?title=(.+)$  page/$1.php  [R, L]

您应该使用.htaccess文件。 添加:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^picture/([a-zA-Z0-9]+).php$ pictures.php?title=$1
</IfModule>

如果您使用的是WAMP之类的本地服务器,请确保打开apache的de mod_rewriterewrite_module并重新启动服务器。

您还应该使用Google for Regular Expressions。 让我知道它是否有用。

暂无
暂无

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

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