如何在 Apache 2.4 中重定向 /path/variables/ 以重写为 /index.php/path/variables/?
Posted
技术标签:
【中文标题】如何在 Apache 2.4 中重定向 /path/variables/ 以重写为 /index.php/path/variables/?【英文标题】:How can I redirect /path/variables/ to rewrite to /index.php/path/variables/ in Apache 2.4? 【发布时间】:2014-12-15 19:53:14 【问题描述】:我最近将本地开发服务器从 Apache 2.2 升级到了 Apache 2.4(通过 Mac OS X 10.10 Yosemite)。本地主机按预期解析,但以下虚拟主机设置不再起作用(返回 Not Found - The requested URL /subdir was not found on this server.
)。
您能帮我将以下从 Apache 2.2 升级到 Apache 2.4 吗?
目标是使用 PATH 变量将所有目录重定向到根目录。例如:将/abc/def/
重定向到/index.php/abc/def/
。单独的索引工作正常。但是/abc/
或/abc/def/
(或任何子目录(路径)不再起作用。
<VirtualHost *:80>
ServerName mylocalsite.local
ServerAlias www.mylocalsite.local mylocalsite_alt.local
<Directory /Users/my_username/Sites/mylocalsite/html>
AllowOverride none
Options all
Require all granted
Deny from none
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %HTTP_HOST ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Redirect all /img/abc to /img/index.php/abc
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule ^img/(.*) img/index.php/$1 [L]
# redirect all directories to root using PATH variables
# Ex. /abc/def/ redirects to /index.php/abc/def/
RewriteCond %REQUEST_URI !=/server-status
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule (.*) index.php/$1 [L]
</IfModule>
</Directory>
DocumentRoot /Users/my_username/Sites/mylocalsite/html
</VirtualHost>
你能明白为什么这不能正确解决吗?
其他调试:
> ping mylocalsite.local
PING mylocalsite.local (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.115 ms
^C
--- mylocalsite.local ping statistics ---
1 packet transmitted, 1 packet received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.115/0.115/0.115/0.000 ms
> sudo apachectl configtest
Syntax OK
> httpd -S
VirtualHost configuration:
*:80 is a NameVirtualHost
default server sandbox (/private/etc/apache2/vhosts/virtual_hosts.conf:8)
port 80 namevhost sandbox (/private/etc/apache2/vhosts/virtual_hosts.conf:8)
alias sandbox
port 80 namevhost mylocalsite.local (/private/etc/apache2/vhosts/virtual_hosts.conf:20)
alias www.mylocalsite.local
alias mylocalsite_alt.local
ServerRoot: "/usr"
Main DocumentRoot: "/Users/my_username/Sites"
Main ErrorLog: "/private/var/log/apache2/error_log"
Mutex default: dir="/private/var/run/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex proxy: using_defaults
PidFile: "/private/var/run/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="_www" id=70 not_used
Group: name="_www" id=70 not_used
【问题讨论】:
如果您在浏览器中输入domain.com/index.php/abc/def/
,它会打开正确的页面吗?
@anubhava,没有。它返回 Not Found - The requested URL /subdir was not found on this server.
更新了问题以更清楚地反映这一点。
使用此代码<?php phpinfo(); ?>
创建一个名为info.php
的文件,然后打开http://domain.com/info.php
以检查它的DOCUMENT_ROOT
值是多少。
感谢您为我指明正确的方向。原来我错过了mod_rewrite.so
模块。很明显,我知道。指向第一个发布此内容的人:LoadModule rewrite_module libexec/apache2/mod_rewrite.so
很高兴它成功了,如果需要,您可以将其发布为您问题的答案。
【参考方案1】:
确保在您的httpd.conf
中取消注释以下行:
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
LoadModule php5_module libexec/apache2/libphp5.so
LoadModule alias_module libexec/apache2/mod_alias.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
Include /private/etc/apache2/extra/httpd-vhosts.conf
# restart apache
【讨论】:
以上是关于如何在 Apache 2.4 中重定向 /path/variables/ 以重写为 /index.php/path/variables/?的主要内容,如果未能解决你的问题,请参考以下文章
apache_conf htaccess在Wordpress中重定向上传