【问题标题】:After changing the url in .htaccess php is not working更改 .htaccess 中的 url 后 php 不起作用
【发布时间】:2017-11-09 23:21:13
【问题描述】:

我更改了这个网址 http://localhost/rozgar/job.php?search=Testing%201&id=43 对此 http://localhost/rozgar/job/Testing%201/43 通过使用 .htaccess

RewriteRule ^job/([A-Za-z]+) job.php?id=$1&search=$2

链接工作正常,但 php 没有从数据库加载数据,它显示错误..

    <?php
include "inc/db.php";
$id = @$_GET['id'];
$search = @$_GET['search'];
session_start();
$email = @$_SESSION['email'];

  $id=@$_GET['id'];
  $query="select * from company,jobs where company.id = jobs.company_id AND jobs.id = '$id'";
$run=mysqli_query($db,$query);

while ($row = mysqli_fetch_assoc($run)) {
}

【问题讨论】:

标签: php .htaccess url


【解决方案1】:

你必须试试这个规则。您只为搜索字段传递了值,但您也传递了 id 的值,例如 (0-9)。那么你的 htaccess 规则如下所示。

RewriteRule ^job/([A-Za-z-]+)/([0-9]+) job.php?search=$1&id=$2

在此之后,您可以将其从该网址中提取出来。

 http://localhost/rozgar/job/testing/43

记住一件事,你不应该在 url 中传递值,比如“testing 002”你的 htaccess 规则中断。为此,您必须将空格转换为下划线。

【讨论】:

  • 如何将空格转为下划线?
  • 使用php函数str_replace(" ","_","Hello world!");;
【解决方案2】:

像这样创建.htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^([^/]+)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]


RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ job.php?id=$1&search=$2 [L,QSA]

现在在job.php中获取get变量

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-02
    • 2015-10-08
    • 2014-06-06
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    相关资源
    最近更新 更多