0

recently, I have been working on a small project for a client. I was thinking about using a wordpress style pretty URL format. I googled around a lot and came accross Apache’s mod_rewrite and RewriteRule, RewriteCond. But didn't find any example which can handle random of parameters. For example:

  • /index.php?param=1/index/param/1
  • /index.php?foo=bar&hour=1&minutes=12/index/foo/bar/hour/1/minutes/12
  • /index.php?page=login&attempt=2/index/page/login/attempt/2

or something similar.

As you can see parameters are not fixed. Is there anyway to achieve this? Any help would be greatly appreciate.

Thanks

1

1 Answer 1

1

Most CMS does this with PHP.

.htaccess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

PHP

$URI = substr($_SERVER['REQUEST_URI'], 1);
$URIparts = explode("/", $URI);

Then you've got an array of the parts of the url, and you can process them as you want.

Sign up to request clarification or add additional context in comments.

3 Comments

$_SERVER['REQUEST_URI'] does always exist.
indeed, i just wanted to write those couple lines very quickly. Thanks for pointing it out.
Thanks @galambalazs, I have modified a little bit so that it only redirect certain pages to it's right controller, this will keep all the parameters. RewriteRule ^page/?.*$ ./page.php [L] RewriteRule ^ssl/?.*$ ./ssl.php [L] Then in PHP $URI = substr($_SERVER['REQUEST_URI'], 1); $URIparts = explode("/", $URI);

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.