将页面URL更改为PHP
您好,我想将类似于将页面URL更改为PHP,php,url,Php,Url,您好,我想将类似于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"
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
中工作。通常,从表单发布时,我会创建中间页,但在您的情况下,您的链接如下所示:
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 } }?>
" />
这里有一个很好的答案:
这将告诉您如何屏蔽URL
或者,您可以将这些文件上载到文件结构中名为“page/”的目录中,然后您可以使用www.example.com/page/filename.php访问页面目录中的文件(作为下载)。您应该使用.htaccess文件。添加以下内容:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^picture/([a-zA-Z0-9]+).php$ pictures.php?title=$1
</IfModule>
重新启动发动机
重写基/
重写规则^picture/([a-zA-Z0-9]+).php$pictures.php?title=$1
如果您使用的是像WAMP这样的本地服务器,请确保打开apache的demod_rewrite
或rewrite_module
,然后重新启动服务器
你也应该在谷歌上搜索正则表达式。
让我知道它是否有用。你要这个吗
RewriteEngine on
RewriteRule ^pictures.php?title=(.+)$ page/$1.php [R, L]
看看.htaccess文件是如何工作的,它可以帮助在.htaccess文件中编写此代码,但没有发生任何事情@stoict不理解此@J2D8TMy
picture.php页面
看起来不是这样,但仍然不起作用检查我的帖子我更新了它@j2d8t直到现在它在url/$page\u title.php中给出了此代码
现在它说解析错误:语法错误,第21行C:\xampp\htdocs\mysql\u login\pictures.php中出现意外的“:”
Sorryheader('Location:uploads/$page\u title.php')代码>我只是把代码放在.htaccess文件中,但什么都没发生没有错误什么都没有changed@user3023297当你输入//yoursite.com/picture/my-pic.php(记住用“我的pic”替换现有图像)时,你可以发布一个屏幕截图吗。