【问题标题】:vHost redirect not working at allvHost 重定向根本不起作用
【发布时间】:2015-03-07 07:20:00
【问题描述】:

目标:将每个请求重定向到 index.php,除了 RewriteCond 中列出的文件和文件夹(及其内容)。

一位朋友确实和我一起设置了服务器,但还没有时间修复这个错误。页面自动以 HTTPS 结束。

当将此用作 000-default.conf (/etc/apache2/sites-enabled/000-default.conf) 时,页面不会重定向到 index.php。例如:访问 www.page.com/uploads/38 有效,尽管它应该重定向到 www.page.com/index.php。这很烦人,因为我正在模拟文件系统并且不想允许访问文件,至少不是那样。

a2ensite 000-default.conf:站点 000-default.conf 已启用 a2enmod 重写:模块重写已启用

这是我的 000-default.conf:

<VirtualHost *:80>
  ServerAdmin root@page.com
  DocumentRoot /var/www/default
  ServerName www.page.com
  ServerAlias page.com
  RewriteEngine On
  <Location />
    RewriteBase /
    Options -Indexes
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^(index\.php|css|fonts|gfx|js|favicon\.ico)
    RewriteRule ^(.*)$ index.php [L,QSA]
  </Location>
  LogLevel warn
  ErrorLog ${APACHE_LOG_DIR}/default_error.log
  CustomLog ${APACHE_LOG_DIR}/default_access.log combined
</VirtualHost>

<VirtualHost *:443>
  ServerAdmin root@page.com
  DocumentRoot /var/www/default
  ServerName www.page.com
  ServerAlias page.com
  RewriteEngine On
  <Location />
    RewriteBase /
    Options -Indexes
    Options +FollowSymlinks
    RewriteCond %{REQUEST_URI} !^(index\.php|css|fonts|gfx|js|favicon\.ico)
    RewriteRule ^(.*)$ index.php [L,QSA]
  </Location>
  LogLevel warn
  ErrorLog ${APACHE_LOG_DIR}/default_error.log
  CustomLog ${APACHE_LOG_DIR}/default_access.log combined

  SSLEngine on
  SSLCertificateFile      /etc/ssl/page.com.crt
  SSLCertificateKeyFile   /etc/ssl/page.com.key
  SSLCertificateChainFile /etc/ssl/comodo-ca.crt
</VirtualHost>

在查看 default_error.log 时,我经常会发现这样的内容:

Request exceeded the limit of 10 internal redirects due to probable configuration error.

还有:

RSA server certificate CommonName (CN) `page.com' does NOT match server name!?

提前致谢。

【问题讨论】:

    标签: apache .htaccess redirect vhosts


    【解决方案1】:

    %{REQUEST_URI} 变量以/ 开头,而您的正则表达式并非如此,因此条件始终为真,包括当 URI 被重写为 /index.php 时,这会导致循环。

    将块替换为:

    RewriteBase /
    Options -Indexes
    Options +FollowSymlinks
    RewriteEngine On
    RewriteCond $1 !^(index\.php|css|fonts|gfx|js|favicon\.ico)
    RewriteRule ^(.*)$ index.php [L,QSA]
    

    另外,我猜你已经购买了名称为“page.com”的证书,不是“www.page.com”,这意味着当你访问“www.page”时.com”并且浏览器看到“page.com”的证书,它会抛出异常。您需要“www.page.com”的证书(并且可以选择使用“page.com”作为备用名称)/。


    编辑

    嗯,这对我在 &lt;Location /&gt; 容器中也不起作用。但这本身就在容器之外起作用:

    RewriteEngine On
    
    RewriteCond $1 !^(index\.php|css|fonts|gfx|js|favicon\.ico)
    RewriteRule ^/(.*)$ /index.php [L,R]
    

    【讨论】:

    • 我现在使用了 $1(我们之前已经这样做了,然后测试了 %{REQUEST_URI} 并重新启动了 Apache。仍然是同样的问题,我仍然能够访问 page.com/uploads/38(我最终到达page.com/uploads/38)。
    • @Lutan 哦,你想从字面上重定向浏览器吗?然后您需要在方括号中使用R 标志:[L,R][L,R=301] 进行永久重定向。
    • 我测试了 [L,R] 和 [L,R=301] 并重新启动了 apache(/etc/init.d/apache2 restart),仍然无法正常工作...
    • 我将其更改为您的编辑,现在它重定向了。剩下一个问题:在我进行 serverreset 之前,我使用了模拟路径 && REQUEST_URI(page.com/Lutan/test/stuff.txt 在运行 index.php 时将 /Lutan/test/stuff.txt 输出为 REQUEST_URI)来显示特定数据。它也没有改变网址。有没有简单的方法(或类似的方法)来做到这一点?
    • @Lutan 重定向是一个全新的请求,因此浏览器最初请求的内容都会丢失。
    猜你喜欢
    • 2019-02-12
    • 2014-11-25
    • 2015-10-09
    • 2021-06-16
    • 2016-03-24
    • 2017-06-23
    • 1970-01-01
    • 2012-09-10
    • 2014-03-14
    相关资源
    最近更新 更多