简体   繁体   English

如何使用 .htaccess 重写 URL

[英]How to re-write URL with .htaccess

I am trying to re-write:我正在尝试重写:

news.php?q=Contact+form+with+attachment

to look like:看起来像:

news/Contact+form+with+attachment

my link looks like this我的链接看起来像这样

<a href="news/'.urlencode($row['title']).'">

and appears like this并且看起来像这样

http://localhost/untitled/public_html/news/Contact+form+with+attachment

in my news.php file , I have this code at the top在我的news.php file ,我在顶部有这个代码

<?php $id = urldecode($_GET['q'])
$id = preg_replace('#[^0-9a-zA-Z]#i','',$id);
     $query = "SELECT * FROM news WHERE title = :id" ?>;

then in my .htaccess I wrote this然后在我的 .htaccess 中我写了这个

RewriteRule ^news/([0-9a-zA-Z_%+-]+) news.php?q=$1 [NC,L]

But when I click on the link it doesn't work it shows an empty/blank page please help thanks但是当我点击链接时它不起作用它显示一个空/空白页面请帮助谢谢

it looks like you might be getting a white blank page because of a missing ;看起来您可能会因为丢失而得到一个白色的空白页; in your php at the end of this line <?php $id = urldecode($_GET['q'])在这一行末尾的 php 中<?php $id = urldecode($_GET['q'])

I don't know if that's your whole problem, but start by adding that like this:我不知道这是否是您的全部问题,但请先添加如下内容:

<?php 
  $id = urldecode($_GET['q']);
  $id = preg_replace('#[^0-9a-zA-Z]#i','',$id);
  $query = "SELECT * FROM news WHERE title = :id" ?>;

The first variable $id wasn't correctly closed.第一个变量$id没有正确关闭。 Maybe the page is returning an empty or blank space because it's a fatal error.也许页面返回空白或空白区域,因为这是一个致命错误。 Change your code for this:为此更改您的代码:

<?php 
$id = urldecode($_GET['q']);
$id = preg_replace('#[^0-9a-zA-Z]#i','',$id);
$query = "SELECT * FROM news WHERE title = :id";
?>

Sorry for my english.对不起我的英语不好。

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

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