我正在尝试将其他index.php文件的所有请求转发到根index.php文件,并将路径编码为GET变量。以下是我的意思的一些例子:
http://my-site.com/this/is/the/path/index.php将成为: http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2Findex.php
http://my-site.com/this/is/the/path/将成为: http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2F
http://my-site.com/this/is/the/path/index.php?v=var将成为: http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2Findex.php%3Fv%3Dvar
如何使用.htaccess文件完成此操作?
修改 要清楚,这就是我希望实现的目标:
总之,只应重定向到index.php文件(不是根目录)或以斜杠结尾的路径。
答案 0 :(得分:0)
您可以使用此.htaccess文件
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?path=$1 [L]
</IfModule>
答案 1 :(得分:0)
我建议在Apache的mod_rewrite中学习一点regular expressions。
RewriteEngine On
RewriteRule ^(.*/index.php)$ index.php?path=$1
请注意,这只会重写在index.php中结尾的URL,并保留所有其他URL,因为这是您指定的行为。如果要将所有URL重写为索引:
RewriteEngine On
RewriteRule ^(.*)$ index.php?path=$1
答案 2 :(得分:0)
将此代码放入DOCUMENT_ROOT/.htaccess
文件中:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?path=$1 [L,QSA]