PhilipAbbott
PhilipAbbott

Reputation: 1

.htaccess to parent directory

I am trying forward /test/12 to test?var=12 using htaccess

I have set up my .htaccess file as follows:

RewriteEngine on
RewriteRule ^test/(.+)$ test?$1 [L]

Can somebody point me in the right direction?

Thanks

Upvotes: 0

Views: 66

Answers (2)

PhilipAbbott
PhilipAbbott

Reputation: 1

My mistake was adding the .htaccess in the wrong directory. I should have added it in the parent directory, but I was adding ^test/(.+)$ test?$1 in the test directory.

I wanted to take the full query after the trailing slash in the url, so the rule I used in the parent directory was RewriteRule ^(.*)$ test.php?$1. This in turn caused a circular loop so I redirected to a directory within the parent so the final working rule that took the final part of the url string after the / and forwarded it as a variable to test.php?$1 looked like this.

RewriteRule ^(.*)$ test/test.php?$1

Upvotes: 0

Ronak Dhoot
Ronak Dhoot

Reputation: 2321

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^test/([a-zA-Z0-9|]+)/([^/]*)/(.*)$ test.php?var1=$1&var2=$2&var3=$3 [B,QSA]

[^/] means "any character that's not a slash". Naturally this means that "text" cannot contain any slashes, but your URL will be matched correctly.

Also note the [B] which is one of many options you can add to a rewrite rule. [B] means that any &s and some other characters will be escaped.

now in your test.php

print_r($_GET);

will output for url http://localhost/test/123/456/asdf:

Array
(
    [var1] => 123
    [var2] => 456
    [var3] => asdf
)

Upvotes: 1

Related Questions