.htaccess 重定向和 RewriteRule 的问题(有或没有 RewriteCond)


problems with .htaccess redirection and RewriteRule (with or without RewriteCond)

我必须设置网址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]

重写是强制性的吗?如果没有,我会错过什么?

谢谢!

编辑: .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]

您的my-test-script.php是否位于.htaccess的同一级别?如果是这样,只需执行以下操作:

RewriteEngine On
RewriteBase /
RewriteRule ^test/?$ my-test-script.php [NC,L,QSA]
RewriteEngine On
RewriteBase /
RewriteRule ^/test/?$ /my-test-script.php [NC,L,QSA]

试试吧。这在我拥有的测试环境中对我有用。