这是我重写的规则:
RewriteEngine on
RewriteRule ^dev/([^/]+)/ dev/index.php?test=$1 [NC]
然而,与其将url从www.test.com/dev/asdf
更改为www.test.com/dev/index.php?test=asdf
,相反,我们得到了www.test.com/index.php?test=asdf
。因此,重写规则中的dev/
部分基本上跳过了。
其预期效果是将dev/variable/
解析为get变量,从而将其更改为dev/index.php?test=variable
。
发布于 2014-06-29 04:01:11
这可能是几件事:.htaccess
在错误的文件夹中,不正确的RewriteBase设置在其他地方,或者url正在被另一条规则重写。
确保将.htaccess
放在您的DOCUMENT_ROOT
文件夹中(高于dev
)。
然后试试这个:
RewriteEngine on
RewriteBase /
RewriteRule ^dev/([^/]+)/ dev/index.php?test=$1 [NC,L]
发布于 2014-06-29 04:13:44
尝试以下mod_rewrite
规则集:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(dev/index.php)$ [NC]
RewriteRule ^dev/?([^/]*)$ /dev/index.php?test=$1 [R,L,NC]
包含RewriteCond
的第二行是确保没有无休止的dev/[something]
循环到dev/index.php
,这将导致内部服务器错误。R
标志设置了一个真正的重定向到/dev/index.php?test=
,而不仅仅是在幕后传递参数。
R
标志的一个好处是使用命令行中的curl -I
调试这些内容,以显示返回的实际标题。我正在使用localhost:8888
在我当地的MAMP,FWIW:
curl -I localhost:8888/dev/asdf
现在产生的头如下所示:
HTTP/1.1 302 Found
Date: Sun, 29 Jun 2014 04:15:31 GMT
Server: Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8y DAV/2 PHP/5.4.10
Location: http://localhost:8888/dev/index.php?test=asdf
Content-Type: text/html; charset=iso-8859-1
HTTP/1.1 302 Found
意味着将发生一个302
临时重定向。Location
头显示http://localhost:8888/dev/index.php?test=asdf
的正确最终目的地。
然后,在我的测试设置中,我放置了一个dev/index.php
文件,其中包含以下简单的PHP代码:
<?php
echo '<pre>';
print_r($_GET);
echo '</pre>';
?>
因此,对localhost:8888/dev/asdf
的调用创建了http://localhost:8888/dev/index.php?test=asdf
的最终URL,该asdf
脚本的输出显示asdf
正在按需要正确地传递:
Array
(
[test] => asdf
)
https://stackoverflow.com/questions/24473013
复制相似问题