velop
velop

Reputation: 3224

Using htaccess to pass directories to GET parameters Without rewriting url

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]

Upvotes: 1

Views: 2561

Answers (2)

velop
velop

Reputation: 3224

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.

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143906

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. .

Upvotes: 2

Related Questions