I'm using Apache 2.2 and PHP 5.3.8. How should I configure .htaccess to redirect every request do index.php?

I'd like to have URLs like those of stackoverflow or facebook, as: http://mywebsite.com/fancypage where fancypage isn't a directory on the server but just a parameter.

So I thought I needed to redirect every request to a php page, where some code can interpret the $_SERVER['REQUEST_URI'] string. Is this the right way? will $_SERVER['REQUEST_URI'] still be available after the redirection?

有帮助吗?

解决方案

Try adding the following to the .htaccess file in the root directory of your site.

It will send all requests (except for existing files/dirs) to index.php.

The original request will be available in the request param.

RewriteEngine On
RewriteBase /

#skip existing files or directories
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
#everything else goes to index.php
RewriteRule ^ index.php?request=%{THE_REQUEST} [L]

其他提示

What you are interested in is Apache's url_rewrite function. I suggest you visit these two links and research it a bit:

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top