【问题标题】:Using htaccess to pass directories to GET parameters Without rewriting url使用 htaccess 将目录传递给 GET 参数而不重写 url
【发布时间】:2013-10-22 14:44:13
【问题描述】:

我想传递(而不是重定向)这样的东西:

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,参数与上述相同。

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

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]

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    你想去掉最后两条规则中的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。 .

    【讨论】:

    • 你是对的,但我必须处理不在 example.com/foo/bar/js/bla.js 而是在 example.com/js/bla.js 的外部资源。有什么办法可以规避这个问题?我遇到的另一个问题是子域 testumgebung 被重定向到maskesuhren.de/testumgebung.html,但我想将它保留在 testumgebung.maskesuhren.de。感谢您到目前为止的帮助。
    【解决方案2】:

    我必须改变顺序才能达到我想要的效果

    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,使外部资源以斜线开头。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      相关资源
      最近更新 更多