【发布时间】:2013-07-07 10:25:20
【问题描述】:
我正在迁移到一个不允许访问编辑 httpd.conf 或 vhosts.conf 的新主机,因此我需要改用 .htaccess。
这是我现有的虚拟主机配置:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.dev
DocumentRoot /path/to/html
Alias /sitemap.xml /path/to/html/sitemap.php
Alias /sitemap.xml.gz /path/to/html/sitemap.php
AliasMatch ^/sitemap(.*).xml /path/to/html/sitemap.php
AliasMatch ^/sitemap(.*).xml.gz /path/to/html/sitemap.php
Alias /robots.txt /path/to/html/robots.php
AliasMatch ^/robots.txt /path/to/html/robots.php
<Directory /path/to/html>
AllowOverride none
Options all
Order allow,deny
Allow from all
Deny from none
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
# The following redirects 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>
</VirtualHost>
目标是使用 htaccess 获得相同的交互。
我正在寻找的主要互动是:
sitemap.xml => sitemap.php
sitemap-abc.xml => sitemap.php
sitemap-___.xml => sitemap.php
sitemap-xyz.xml => sitemap.php
robots.txt => robots.php
/abc/def/ => /index.php/abc/def/
上面的 vhosts.conf 在我的旧主机上运行良好,但我不确定如何在只有 htaccess 的新主机上执行它。
【问题讨论】:
-
Em...您想在 .htaccess 文件中定义虚拟主机?如果甚至没有定义主机,服务器应该如何知道它是用来扫描您打包 .htaccess 文件的文件夹?
-
你是说不能使用 htaccess 而不是虚拟主机来修改这些模式?
-
啊抱歉,我好像没理解你的目标......是的,当然你也可以在
.htaccess样式文件中进行大多数类型的重写和重定向。它只是第二选择,在少数情况下更复杂,但你别无选择。但是我看不出您的实际问题是什么:所需的重写规则看起来很简单,就像优秀文档 mod_rewrite 中给出的示例一样。你到底在哪里遇到问题? -
只要您要重写的所有内容都在文档根目录中,使用 mod_rewrite 就可以了。
-
原来是我的主机的 fastcgi 问题。粘贴在 mod_rewrite 命令中,我很高兴。
标签: apache .htaccess mod-rewrite virtualhost httpd.conf