【问题标题】:Deployment Server Ignores .htaccess File部署服务器忽略 .htaccess 文件
【发布时间】:2017-11-13 04:26:55
【问题描述】:

我在根目录下的.htaccess 文件如下所示:

RewriteEngine on
RewriteBase /
options +FollowSymLinks
RewriteRule ^en/users/profile/?$ [F,NC]
RewriteRule ^en/users/profile/?([0-9]+)?/?$ en/user/index.php?id=$1
RewriteRule ^en/users/profile/([0-9]+)/photo/? en/user/index.php?pageID=photoEdit&id=$1

RewriteCond %{HTTP_REFERER} !^http://example\.ge/?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example\.dev/?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://localhost/?.*$ [NC]
RewriteRule \.(gif|jpe?g|png|bmp)$ - [F,NC]

它可以在本地主机上完美地重新路由,但在部署服务器上被忽略!这是 服务器的phpinfo();

这是 etc 文件夹:

【问题讨论】:

  • 从您的phpinfo() 输出看来,部署环境正在运行LiteSpeed Web 服务器而不是Apache。你可能想看看这个链接:internetearnings.com/…
  • 您的第一个 RewriteRule 指令缺少 替换 并且无法按预期工作 - 如果请求 /en/users/profile/,您将收到错误消息。但是您实际上并没有说明您要路由的 URL。
  • 没错。请提供答案,以便我接受。亲爱的怀特先生

标签: php .htaccess mod-rewrite web-deployment


【解决方案1】:
RewriteRule ^en/users/profile/?$ [F,NC]

不是您的 .htaccess 文件被忽略的原因(其他答案已经给出了解决方案,关于检查服务器配置中的 AllowOverride 指令),但您缺少一个有效的 substitution 在上述指令中。对/en/users/profile/ 的请求不会被禁止,并且可能只会导致错误/404。

应该是这样的:

 RewriteRule ^en/users/profile/?$ - [F,NC]

替换只需一个连字符。)

RewriteCond %{HTTP_REFERER} !^http://example\.ge/?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://example\.dev/?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://localhost/?.*$ [NC]
RewriteRule \.(gif|jpe?g|png|bmp)$ - [F,NC]

您还应该将“热链接保护”指令(上图)移至路由指令之前。否则,有可能(取决于 URL 格式)在热链接保护能够阻止请求之前路由 URL。

【讨论】:

    【解决方案2】:

    编辑您的虚拟主机:

    1. 登录到您的服务器。

    2. cd /etc/apache2/sites-available/

    3. 您可以编辑 defaultweb1 或 ... virtual-host-.conf -文件。输入 nano web1.conf (...) 并编辑您的文件。

    <VirtualHost *:80>
        ServerName yoururl.com      
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/web1/htdocs/
        <Directory /var/www/web1/htdocs/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride all
                Order allow,deny
                allow from all
        </Directory>
        ErrorLog /var/log/apache2/error.log
        LogLevel warn
        CustomLog /var/log/apache2/access.log combined
        ServerSignature On
    </VirtualHost>
    

    4. 停止 Apache2 并重新启动它 - /etc/init.d/apache2 stop 和 /etc/init.d/apache2 start。

    5. 将您的 .htaccess 上传到您的根目录并进行测试。

    如果您使用 Cloudflare,则在 SSL 设置 中的设置“始终使用 HTTPS”中有一个选项。您还可以使用转发 URL 操作

    http://yoururl.fqdn/*
    redirected with a 301 response code to
    https://www.yoururl.fqdn/$1
    

    【讨论】:

    • 谢谢。但它是一个共享服务器。
    • 我已经更新了问题,其中包含指向 etc 文件夹预览的链接。
    • 那么,您无法通过 SSH 访问?您无法通过 PuTTY 或类似的方式连接您的服务器?
    • Noap!无法通过 ssh 访问。
    • 您可以将 htaccess 一次上传到您的网站空间,其中存储了 HTML 页面吗?
    【解决方案3】:

    您可能没有在服务器上启用.htaccess 文件。看看AllowOverride 指令。您可以在&lt;VirtualHost&gt; 的文档根目录的&lt;Directory&gt; 块中添加类似的内容:

    AllowOverride all
    

    【讨论】:

    • 这是您需要解决的问题!祝你好运。
    • 您希望我帮助您解决无法对服务器进行任何更改的事实,解决此问题时涉及对服务器进行更改??????我已经帮助过你,告诉过你该怎么做,在你所处的环境下弄清楚如何去做,这不是这里任何人可以做的。
    • 这是免费的帮助,你粗鲁无礼。我已经给了你一些建议,你可以尝试通过谷歌自己理解。本次对话结束。
    • 哦,我很抱歉。我不是故意粗鲁的。请接受我的道歉!可能是因为我的英文不太好!真的很抱歉!
    • 已接受道歉。我不知道在 cPanel 的哪里可以找到它,但是如果您在 Google 上搜索“cpanel allowoverride”,我相信您会找到答案。
    猜你喜欢
    • 2011-07-09
    • 1970-01-01
    • 2013-01-13
    • 2017-12-17
    • 2012-09-20
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多