Qqwy
Qqwy

Reputation: 5639

How to redirect URLs so that they are all handled by the same PHP page

I know that there are many questions about this subject, but the questions, and even more the answers are kind of confusing me.

What I want to do: I want to have an internet page, wich, depending on the URL, shows different content. However, in the backend, all pages are handled by one central PHP page.

In other words:

www.example.com/

www.example.com/AboutUs

www.example.com/Contact

should all be handled by a single .php script, but in such a way that in the browser of the users the URLS are kept intact.

Now, is this done with .htaccess rewriting or not? And how?

Upvotes: 0

Views: 74

Answers (2)

Jason
Jason

Reputation: 339

Yes, you can achieve this by adding mod_rewrite rules to your .htaccess file. Here is an article with more detailed information: http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/.

It may not help your confusion, but it will at least teach you the proper syntax. Basically, mod_rewrite takes the "clean" URL given in the browser, decodes it using a regular expression, then discretely passes the matches from the regular expression as GET variables.

In other words: mod_rewrite takes "example.com/AboutUs", reads the URL, and serves up whatever would be on the page "example.com/index.php?page=AboutUs" without showing users the actual GET-variable-ridden URL.

Upvotes: 1

webbiedave
webbiedave

Reputation: 48897

.htaccess using Rewrite would be the best approach for this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>

In your index.php you can use the value of $_GET['uri'] or $_SERVER['REQUEST_URI'] to determine which functionality is being requested.

If you only want your controller script to handle requests for files and directories that don't already exist, you can do:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>

Upvotes: 3

Related Questions