将页面URL更改为PHP


Changing Page URL To PHP

嗨,我想将类似www.example.com/pictures.php?title=yyoy的页面URL转换为类似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 中工作

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

或者,您可以将这些文件上传到文件结构中名为"page/"的目录中,然后您可以通过www.example.com/page/filename.php

访问页面目录中的文件(作为下载)

通常我会在从表单发布时创建一个中间页面,但在您的情况下,您的链接看起来是这样的:

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

因此,它会引导您访问pictures.php,URL中有一个变量"title"。

因此,在你的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 } }?>

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的demod_rewriterewrite_module并重新启动服务器。

你还应该在谷歌上搜索正则表达式。让我知道它是否有用。