【问题标题】:problems with .htaccess redirection and RewriteRule (with or without RewriteCond).htaccess 重定向和 RewriteRule 的问题(有或没有 RewriteCond)
【发布时间】: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 是强制性的吗?如果没有,我错过了什么?

谢谢!

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


【解决方案1】:

您的my-test-script.php 是否与您的.htaccess 位于同一级别?如果是这样,请这样做:

RewriteEngine On
RewriteBase /

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

【讨论】:

  • .htaccess 和 my-test-script.php 在同一个文件夹中(我将编辑我的问题)。遗憾的是,您的解决方案不起作用。
  • example.com/test(遗憾的是我目前无法共享一个真正的公共 url,但它具有完全相同的结构 - 协议、域、路径)
  • 哦,是的,对不起:http://subdomain.example.com/test。无论如何,我也尝试了主域,我得到了相同的结果
  • 没有。我还尝试从/client/project_name/domain/subdomain/ 开始,每次尝试删除一个级别。 Apache 的主 DocumentRoot 是/var/www/html/。域example.com 设置在/var/www/html/client/project_name/domain/ 下。 subdomain.example.com 指向/var/www/html/client/project_name/domain/subdomain/
【解决方案2】:
RewriteEngine On
RewriteBase /

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

试试吧。这适用于我拥有的测试环境。

【讨论】:

  • 它不起作用:Not Found The requested URL /test was not found on this server.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 2015-01-27
  • 2012-04-22
  • 1970-01-01
相关资源
最近更新 更多