【发布时间】: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§ion=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§ion=$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