问题原文
原帖链接 · markzz · CC BY-SA 3.0
I have a project that reads the $_SERVER['PATH_INFO'] variable when loading pages, therefore it reads pages in the format of http://address/index.php/pagename and I would like to use mod_rewrite to remove the index.php part of the url and make the previous URL http://address/pagename. If one visits http://address/pagename, it needs to load the page as if that person visited http://address/index.php/pagename.
With Nginx, the following configuration works and does the rewrite...
server {
listen 80;
server_name localhost;
root /srv/http/web/html;
index index.php;
location ~ ^/[^/]+\.php($|/) {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(/[^/]+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
location ~ .* {
rewrite ^/(.*)$ /index.php/$1 last;
}
}
How would I accomplish this in apache. I currently have it working so that the root displays correctly and writing urls with the index.php part works. How can I make the rewrite rule work and still be able to use the $_SERVER['PATH_INFO'] variable? What could I put in the .htaccess to make it work?
已采纳解答
原帖链接 · Anubioz · CC BY-SA 4.0
You cannot directly change $_SERVER["PATH_INFO"] variable from within apache when php is compiled like a module.
But here's a workaround that will do the job just fine:
Create a
rewrite_pathinfo.phpfile with the following contents:<?php if (!empty($_SERVER['PATH_INFO'])) header("Location: " . $_SERVER['PATH_INFO']); $_SERVER['PATH_INFO'] = $_SERVER['REDIRECT_ORIGINAL_PATH']; ?>Put this into your
.htaccess:php_value auto_prepend_file "/var/www/vhosts/path_to_your/rewrite_pathinfo.php" RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.*) /index.php [QSA,L,PT,E=ORIGINAL_PATH:/$1]
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
