I'd like to ask how can I call the page www.domain.com/index.php?page=somepage and make the browser show the user in the URL bar the address www.domain.com/somepage/? How do I do that trick?

  • I saw that you have 3 really good answers here - did they not give you what you needed? – Christopher Feb 14 '11 at 19:11
  • A very important first step not mentioned in the answers below and something which often gets overlooked: The first thing you must do is change the actual URLs you link to in your application - you want to link to the "smarter" (virtual) URLs. .htaccess is then used to convert the "smarter" URLs to the real URL. If you don't do this then every request will result in an external redirect (if that part is implemented) which is undesirable. – MrWhite Oct 31 '15 at 19:17

You'll need a webserver module to do this. If you are running Apache, for example, you can use mod_rewrite to accomplish these type of URLs.

For the .htaccess file you need basic knowledge of regular expressions.

A good start:

| improve this answer | |
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?page=$0  [PT]

As mentioned by Michele, you'll need Apache to do this with mod_rewrite. The above will send all page requests to index.php?page=[somepage], unless the file or directory exists (for things like images).

But you may also want to something more specific so all requests don't get sent to index. You would do something like:

RewriteEngine On
RewriteRule /page1 index.php?page=page1  [L]
RewriteRule /page2 index.php?page=page2  [L]

Although this can get messy if you have a lot of pages, so then you might want something like:

RewriteEngine On
RewriteRule /([a-z0-9_]+) index.php?page=$1  [L]

This will only send all the requests that match the regular expression ([a-z0-9_]+) to index. This would be things like page1, page2, but not section/page1.

| improve this answer | |
RewriteEngine on
RewriteRule ^somepage/([^/\.]+)/?$ index.php?somepage=$1 [L]

open up .htacess and put that code in there.

read more here http://www.workingwith.me.uk/articles/scripting/mod_rewrite

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.