【问题标题】:htaccess rule - Take the Place of Symlinked Fileshtaccess 规则 - 代替符号链接文件
【发布时间】:2012-07-11 00:28:40
【问题描述】:

我正在处理这个遗留项目,它有一个相当奇怪的设置,我希望摆脱它,但我的 htaccess 技能在这个部门有点缺乏。

这是目录结构。

/index.php
/www
  page1.php -> symlink to index.php
  page2.php -> symlink to index.php
  page3.php -> symlink to index.php

/www 是公共目录,人们访问http://site/page1.php。但是,每个带有 * 的文件实际上都是指向 /index.php 的符号链接。

关于可以解决此问题的 htaccess 规则的任何想法?在最基本的核心上,我希望保留相同的功能,而不必拥有一千个符号链接文件。

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    看起来index.php 文件在您的文档根目录中不是(我假设它是www),因此,我认为您没有办法可以从您的 .htaccess 文件中执行此操作。为了访问文档根目录之外的内容,您需要在服务器配置或虚拟主机配置中设置别名:

    # Somewhere in vhost/server config
    Alias /index.php /var/www/path/to/index.php
    
    # We need to make sure this path is allowed to be served by apache, otherwise
    # you will always get "403 Forbidden" if you try to access "/index.php"
    <Directory "/var/www/path/to">
            Options None
            Order allow,deny
            Allow from all
    </Directory>
    

    现在您应该可以访问/var/www/path/to/index.php。请注意,/var/www/path/to 目录中的其他文件是安全的,只要您不创建指向它们的Alias(或AliasMatchScriptAlias)。现在您可以通过 /index.php URI 访问 index.php,您可以在文档根目录 (www) 的 .htaccess 文件中设置一些 mod_rewrite 规则,以将内容指向 index.php:

    # Turn on the rewrite engine
    RewriteEngine On
    
    # Only apply the rule to URI's that don't map to an existing file or directory
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # Rewrite all requests ending with ".php" to "/index.php"
    RewriteRule ^(.*)\.php$ /index.php [L]
    

    这样当你请求 http://site/page1.php 时,浏览器的地址栏没有改变,但服务器实际上服务于/index.php,别名为/var/www/path/to/index.php .

    如果需要,您可以将正则表达式 ^(.*)\.php$ 调整为更合适的值。这仅匹配以.php 结尾的任何内容,包括/blah/bleh/foo/bar/somethingsomething.php。如果要限制目录深度,可以将正则表达式调整为^([^/]+)\.php$等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-28
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      相关资源
      最近更新 更多