如何操作URL中的查询输出?

时间:2015-01-15 07:39:17

标签: php regex .htaccess url-rewriting

在本网站中,当您选择一个类别并输入一个邮政编码时,它会根据邮政编码的地理位置与最近的供应商匹配。表单中使用的方法是GET,因此它将查询传递给URL。

example.com/contractor_page.php?cat=2&zip=75000

我尝试从contractor_page.php寻找main_directory,但无处可寻。我还查看了sub_folders并挖掘了所有代码,并且根本找不到链接此.php文件的引用。

表单操作文件根本不需要存在吗?我尝试操作表单操作的名称,以查看它是否会在搜索后反映在URL中,但它不会继续保持与contractor_page.php相同,并且它驱使我疯狂。

我希望绕过所有这一切,并通过htaccess进行,但我查看多个网站的所有尝试以及与此网站相关的问题均无效。

我想改变它:

example.com/contractor_page.php?cat=2&zip=75000

要:

example.com/Dallas-Fort-Worth/HVAC

其中zip=7500 = dallas-fort-worth

cat=2 = HVAC

当前.htaccess文件如下所示。

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Redirect /index.php to / (optional, but recommended I guess)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php
RewriteRule ^index.php/?(.*)$ $1 [R=301,L]

# Run everything else but real files through index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]

RewriteCond %{QUERY_STRING}   ^cat=2&zip=(.*)$ 
RewriteRule ^contractor_page.php$ /Dallas-Fort-Worth/ [NC,R=301,L]
</IfModule>

header.php文件包含此代码

<?php
  if($uri == "contractor_page.php") {
  ob_start();
    session_start();
    $_SESSION['cat'] = $_GET['cat'];
    $_SESSION['zip'] = $_GET['zip'];

  } else {
   session_start();
  }
 ?>
 <!DOCTYPE html>

表单代码的顶部是:

<form action="contractor_page.php" method="get">
     <div class="form-group">

显示结果的内部页面

 <div class="inner-cont-wrap">
            <?php echo $content; ?>
            <?php if($uri == "contractor_page.php"): ?>
               <div class="yellow-header">
                   <h2>Register Once And You're Covered</h2>
                   <span class="rbn left-ribbon"></span>
                   <a href="#" class="reg-now" onclick="registration=window.open('registration_form.php', 'registration', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, dependent=yes, width=750, height=650, screenX=250, left=250, screenY=100, top=100'); return true;">Register Now</a>
                   <span class="rbn right-ribbon"></span>
                   <p>By Our $10,000 Guarantee!</p>
               </div><!-- end yellow header -->
                <?php  include('_GCL_sc1ipt5/contractor.php'); ?>
            <?php elseif($uri == "contractor_spe.php"): ?>
                <?php include ('_GCL_sc1ipt5/spec_contractor_inc.php'); ?>
            <?php elseif($uri == "contractor_list.php"): ?>
                <?php include ('_GCL_sc1ipt5/contractor_by_category.php'); ?>
            <?php endif; ?>

我将如何开展这项工作?谢谢你们。

1 个答案:

答案 0 :(得分:0)

您可以在RewriteBase行:

下方使用此规则
RewriteCond %{QUERY_STRING} ^cat=2&zip=75000$ 
RewriteRule ^contractor_page\.php$ /Dallas-Fort-Worth/HVAC? [NC,R=301,L]