【问题标题】:htaccess with multiple parameters not working,?具有多个参数的 htaccess 不起作用,?
【发布时间】:2015-03-25 02:41:55
【问题描述】:

我正在尝试使用 htaccess 文件来重写 url,我在这方面的经验真的是零,我所做的一切都是在这里和谷歌上搜索的。我的链接中有查询参数。我将所有内容发送到 index.php 页面。

我在尝试重写之前测试了所有链接,它们都可以在索引页面中使用。因此,我创建了一个 htaccess 文件来重写链接,但我得到的只是一个空白页面,没有任何变量被发送到索引页面。我在 index.php 页面中使用 $_GET 来检索变量。

这是我尝试重写的链接类型列表以及我的实际 htaccess 文件的副本,mod rewrte 也已打开并在 apache 中正常运行。请根据我在下面显示的示例链接帮助我更改我的 htaccess 文件以完全按照我想要的方式进行操作,好吗?

目录重写很好并且可以相应地工作,但链接没有。

此外,如果您注意到 links #1#2 中的 page 参数不会也不应该出现在 这 2 个 链接的重写 链接ONLY 但仍将空白或空字符串发送到我的index.php。

例子--

<?php

$page= $_GET['page'];
$user= $_GET['user'];
$act= $_GET['act'];
$section= $_GET['section'];
$search= $_GET['search']
$xx= $_GET['xx'];

if($page == ''){
   //Do some code
}elseif($page == 'search'){
   //Do some code
?>

重写这些类型的链接

注意 - “http://”是所有这些链接的前面,堆栈溢出不允许我添加和发布。


链接 #1

localhost/notetag2/johnsmith/all/1

--重写到--

localhost/notetag2/index.php?page=&user=johnsmith&act=all&section=1


链接 #2

localhost/notetag2/johnsmith/all

--重写到--

localhost/notetag2/index.php?page=&user=johnsmith&act=all


链接 #3

localhost/notetag2/search/johnsmith

--重写到--

localhost/notetag2/index.php?page=seach&search=johnsmith


链接 #4

localhost/notetag2/pictures/1

--重写到--

localhost/notetag2/index.php?page=pictures&xx=1


重写目录

页面

--重写到--

网络


这是我的实际 HTACCESS 文件

<IfModule mod_rewrite.c>

    RewriteEngine on
    RewriteBase /notetag2/
    RewriteRule ^index\.php$ - [L]

    RewriteRule ^(.*)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=$1&user=$2&act=$3&section=$4 [L,NC]

    RewriteRule ^(.*)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=$1&user=$2&act=$3 [L,NC]

    RewriteRule ^(.*)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=$1&user=$2 [L,NC]

    RewriteRule ^(.*)/$ notetag2/index.php?page=$1 [L,NC]

    RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=search&q=$1 [L,NC]

    RewriteRule ^pictures/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=picturesh&xx=$1 [L,NC]

    RewriteRule ^([a-zA-Z0-9_-]+)/?$ $1.php [L,NC]


    RewriteRule ^pages/(.*) web/$1 [L]


    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* /notetag2/index.php [L,QSA]

</IfModule>

【问题讨论】:

  • 为什么您的 htaccess 文件显示“notetag2”,但您的问题是要从/向“notetag”重写内容?他们应该是不同的吗?
  • 不,它们并没有什么不同。他们是一样的。我不使用复制和粘贴。我把所有的东西都打出来了,只是在这里打错了。现在在我的帖子里已经改了。说notetag2而不是notetag。你能帮忙解决我的问题吗?
  • 你的 htacces 文件在哪里?
  • 我的 htaccess 文件位于:localhost/notetag2 或者更准确地说,它位于:C:\Users\MyComputer\Documents\xampp\htdocs\notetag2 我在我的计算机上使用 xampp。
  • htaccess 的工作原理是我已经尝试过并创建了一行和两行 htaccess 文件并对其进行了测试,它们的工作并不复杂。 mod rewrite 也同样有效。不起作用的是我上面写的htaccess文件,因为它做了一些复杂的事情,我不知道如何在这个文件中正确地完成它们!

标签: php apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

您需要修复几个正则表达式。由于您的 htaccess 文件位于您的 notetag2 目录中,因此您无需匹配前导的“notetag2”。

但首先,您的规则顺序不正确。如果你想匹配/search/something,那么你不能有匹配anything的规则,否则它会匹配/search。所以这些规则需要在顶部:

RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ index.php?page=search&q=$1 [L,NC]
RewriteRule ^pictures/([a-zA-Z0-9_-]+)/?$ index.php?page=picturesh&xx=$1 [L,NC]

您也不需要规则目标中的notetag2/,因为这是隐含的,因为规则位于 notetag2 目录中。

因此,在这两条规则之后,您需要从最大到最小进行其他每个重写:

localhost/notetag2/johnsmith/all/1

--重写到--

localhost/notetag2/index.php?page=tutorial&user=johnsmith&act=all&section=1

你几乎已经有了,只是在正则表达式前面有一些随机的(.*)/,这将使它永远不会匹配你想要的。你想要这个:

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=tutorial&user=$1&act=$2&section=$3 [L,NC]

然后是下一个:

localhost/notetag2/johnsmith/all

--重写到--

localhost/notetag2/index.php?page=tutorial&user=johnsmith&act=all

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=tutorial&user=$1&act=$2 [L,NC]

“搜索”和“图片”应该已经被上面的前两条规则处理好了。

无论您在旧 htaccess 文件底部有什么其他规则,它们都需要遵循这些规则。

所以只剩下:

页面

--重写到--

网络

由于这些不要以 notetag2 开头,您需要将这些规则放在不同的 htaccess 文件中,即文档根目录中的那个:

RewriteRule ^pages/(.*)$ /web/$1 [L]

【讨论】:

  • 如果您在链接 #1 和 #2 中注意到页面参数不会也不应该出现在这两个链接的重写链接中,但空白或空字符串仍应发送到我的 index.php来表示那个变量。前 2 个链接中没有 $page=tutorial ,前 2 个链接中的 $page 变量只应始终包含空字符串。所以你的建议不起作用!。
  • @DavidCunningham 您的问题表明这两个链接的页面变量是tutorial
  • 那是我做其他的时候犯的一个错误,我只是忘记改正了。就像现在一样,我的帖子中的一切都是正确的。请刷新您的浏览器并重新阅读我的帖子,好吗?然后,您会注意到在链接 #1 和 #2 中,页面参数不会也不应该出现在这 2 个链接的重写链接中,但空白或空字符串仍应发送到我的 index.php 以表示该变量。前 2 个链接中没有 $page=tutorial ,前 2 个链接中的 $page 变量只应始终包含空字符串。所以你的建议不起作用!。
  • 然后去掉“教程”
  • 我从前 2 个链接中删除了 page=$1 并移动了 RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ notetag2/index.php?page=search&q =$1 [L,NC] 到顶部。现在一切正常!
猜你喜欢
  • 2013-12-02
  • 1970-01-01
  • 2019-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
  • 2016-02-03
相关资源
最近更新 更多