1

I'm currently trying to create a .htaccess file that essentially converts this:

[From] http://www.example.com/pagename.php?1=name&2=email&3=hash
[To] http://www.example.com/pagename/name/email/hash

Which can then be read in PHP as $name = $_GET[1]; $email = $_GET[2] and so on...

Where pagename is equal to the filename without the file extension and then every trailing slash after that is set as a new GET variable incrementing by one (in a way that you could theoretically define unlimited trailing slashes and it would continue to increment these $_GET variables by one.

Anyone know how?

2
  • No time for "real" answer right now, but: search for PATH_INFO here on SO. Commented Nov 8, 2015 at 19:41
  • Sure, I'll give that a try. Thank you :) Commented Nov 8, 2015 at 19:42

1 Answer 1

1

If you want there to be an arbitrary number of variables, you need to turn on Multiviews then add some code to your php scripts to look at the $_SERVER['PATH_INFO'] variable. So something like this:

$data = explode("/",trim($_SERVER["PATH_INFO"],"/"));

$length = count($data);
for ($i = 1; $i <= $length; $i++) {
  $_GET[$i] = $data[$i-1];
}

to populate the $_GET variable with all the path elements.

Then in htaccess, you need something like this to append the php extension:

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)(/.+)$ /$1.php$2 [L]
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, this works a treat! I will never forget this implementation.
@JohnLin I have a new problem: So far I'm using this HTACCESS file to process my variables: RewriteEngine On RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^([^/]+)(/.+)$ /$1.php$2 [L] This is an example of one of my URLs example.com/browse/search/queryhere Basically I now need to implement this into a form where the form submits and places the GET variable in place of queryhere in the same format as the rest of the URL... Any ideas? I'm getting example.com/browse/search/?q=something which isn't what I need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.