简体   繁体   English

使用mod_rewrite规则将所有路径映射到index.php

[英]A mod_rewrite rule to map all paths to index.php

I have a script at www.mydomain.com/mydirectory/index.php. 我在www.mydomain.com/mydirectory/index.php上有一个脚本。 I want to map all requests to subdirectories of mydirectories to the script. 我想将所有请求映射到脚本的mydirectories的子目录。 For instance, www.mydomain.com/mydirectory/mypath/ should be rewritten to www.mydomain.com/mydirectory/index.php/mypath/ 例如,应该将www.mydomain.com/mydirectory/mypath/重写为www.mydomain.com/mydirectory/index.php/mypath/

How can I accomplish this? 我该怎么做? Thanks. 谢谢。

Try this in the .htaccess file in your mydirectory directory: mydirectory目录的.htaccess文件中尝试以下操作:

RewriteEngine on
RewriteCond $0 !^index\.php/
RewriteRule .* index.php/$0 [L]

This will forward all requests to index.php if a requested file or folder doesn't exist: 如果所请求的文件或文件夹不存在,这会将所有请求转发到index.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

For example, http://www.example.com/category/5/ becomes http://www.example.com/index.php/category/5/ 例如, http://www.example.com/category/5/变为http://www.example.com/index.php/category/5/

You can parse the path behind index.php using explode() and $HTTP_SERVER_VARS 您可以使用explode()和$ HTTP_SERVER_VARS来解析index.php后面的路径

$url = explode('/', $HTTP_SERVER_VARS['PATH_INFO']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM