【发布时间】:2016-02-26 15:13:55
【问题描述】:
我要设置网址http://subdomain.example.com/test,让它能够调用真正的文件/my-test-script.php。
我不明白为什么下面的代码有效:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^\/?test\/?$
RewriteRule ^\/?(.*)$ /my-test-script.php [NC,L,QSA]
虽然这个不起作用:
RewriteEngine On
RewriteBase /
RewriteRule ^\/?test\/?$ /my-test-script.php [NC,L,QSA]
RewriteCond 是强制性的吗?如果没有,我错过了什么?
谢谢!
编辑:
.htaccess 和 my-test-script.php 都在根 / 中。
编辑 2: 有趣的更新:我看到它适用于
RewriteRule test /my-test-script.php [NC,L,QSA]
但不是与
RewriteRule ^test$ /my-test-script.php [NC,L,QSA]
编辑 3: 我创建了一个显示 $_GET 参数的脚本:
RewriteCond %{REQUEST_URI} !^\/?show\-parameters\.php$
RewriteRule ^(.*)$ /show\-parameters.php?path=$1 [NC,L,QSA]
当我打电话给http://subdomain.example.com/test 时,我看到了:
Array ( [path] => /var/www/html/client/project_name/domain/subdomain/test )
编辑 4: 这是我的虚拟主机:
<VirtualHost *>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
RewriteEngine On
### HTTP: from www to non-www ###
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]
<Directory /var/www/html>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order allow,deny
allow from all
RewriteBase /
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName subdomain.example.com
ServerAlias www.subdomain.example.com
DocumentRoot /var/www/html/client/project_name/domain/subdomain
RewriteEngine On
### HTTP: from www to non-www ###
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]
<Directory /var/www/html/client/project_name/domain/subdomain>
Options -Indexes +FollowSymLinks +Multiviews
AllowOverride All
Order allow,deny
allow from all
RewriteBase /
</Directory>
</VirtualHost>
[已解决] 编辑 5:
我只是从 .htaccess 中删除了 RewriteBase / 行,以下规则现在有效:
RewriteEngine On
RewriteRule ^\/?test\/?$ /my-test-script.php [NC,L,QSA]
【问题讨论】:
-
不,它不是目录,.htaccess 位于域的根目录中。在
my-test-script.php的同一个文件夹中 -
Apache/2.4.7. DocumentRoot 是
/var/www/html/client/project_name/domain/subdomain/ -
是的,他们都在
/var/www/html/client/project_name/domain/subdomain/ -
RewriteRule (^|/)test/?$ my-test-script.php [NC,L]工作吗? -
差不多。仅当我在脚本前添加斜杠时才有效:
**/**my-test-script.php。不管怎样,看看我的新EDIT 3,我想我发现了问题:路径包含所有文件夹链。如何解决?
标签: php apache .htaccess mod-rewrite