使用htaccess将目录传递给GET参数而不重写url

时间:2013-10-22 14:44:13

标签: .htaccess mod-rewrite

我想通过(不是重定向)这样的事情:

http://www.example.com/(带/可选) 传递给脚本 http://www.example.com/index.php

http://www.example.com/foo/(带/可选) 传递给脚本 http://www.example.com/index.php?nav=foo

http://www.example.com/foo/bar(带/可选) 传递给脚本 http://www.example.com/index.php?nav=foo&subnav=bar

此外,我想将子域名http://testumgebung.example.com传递给http://www.example.com/testumgebung.php,其参数与上述相同。

我只是设法执行以下操作,但它会将浏览器栏中的网址重写为传递的位置,并且不会像以前那样保留网址:

RewriteCond %{HTTP_HOST}  ^testumgebung\.maskesuhren\.de
RewriteRule ^(.*)$ http://www.maskesuhren.de/$1/testumgebung.html [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ index.html?nav=$1 [R,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /.*/.*
RewriteRule ^([^/]*)/([^/]*)$ index.html?nav=$1&subnav=$2 [R,L]

2 个答案:

答案 0 :(得分:2)

您希望摆脱最后两条规则中的R

RewriteCond %{HTTP_HOST}  ^testumgebung\.maskesuhren\.de
RewriteRule ^(.*)$ http://www.maskesuhren.de/$1/testumgebung.html [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ index.html?nav=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /.*/.*
RewriteRule ^([^/]*)/([^/]*)$ index.html?nav=$1&subnav=$2 [L]

方括号中的R告诉mod_rewrite重定向浏览器,这会更改浏览器位置栏中的URL。

答案 1 :(得分:1)

我必须改变顺序才能实现我想要的目标

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ ?nav=$1 [R,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /.*/.*
RewriteRule ^([^/]*)/([^/]*)$ ?nav=$1&subnav=$2 [R,L]

RewriteCond %{HTTP_HOST} ^testumgebung\. [NC]
RewriteRule ^$ testumgebung.html [L]

并重写html,以便外部资源以斜杠开头。