Using htaccess to pass directories to GET parameters Without rewriting url

老子叫甜甜 提交于 2019-12-22 01:36:34

问题


I would like to pass (not redirect) something like this:

http://www.example.com/ (with / optional) passed to the script http://www.example.com/index.php

http://www.example.com/foo/ (with / optional) passed to the script http://www.example.com/index.php?nav=foo

http://www.example.com/foo/bar (with / optional) passed to the script http://www.example.com/index.php?nav=foo&subnav=bar

Furthermore I would like to pass the subdomain http://testumgebung.example.com to http://www.example.com/testumgebung.php with the same parameters as above.

I just managed to do the following, but it rewrites the url in the browser bar to the passed location and doesn't leave the url as before:

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]

回答1:


You want to get rid of the R in the last 2 rules:

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]

The R in the square brackets tell mod_rewrite to redirect the browser, which changes the URL in the browser's location bar. .




回答2:


I had to change the order to achive what I wanted

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]

and to rewrite the html so that the external resources start with a slash.



来源:https://stackoverflow.com/questions/19521072/using-htaccess-to-pass-directories-to-get-parameters-without-rewriting-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!