redirect every request to index.php and interpret the request

霸气de小男生 提交于 2019-12-14 02:23:50

问题


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?


回答1:


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]



回答2:


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

  • URL Rewriting Guide
  • URL Rewriting for Beginners


来源:https://stackoverflow.com/questions/8995054/redirect-every-request-to-index-php-and-interpret-the-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!