【问题标题】:.htaccess rewriterule path not found while accessing through ip address通过IP地址访问时找不到.htaccess重写路径
【发布时间】:2016-11-09 12:43:26
【问题描述】:

我正在尝试通过 .htaccess 为我的网站设置重写规则。通过域名访问时效果很好 -

http://www.example.com/admin/my/virtual/path

问题是当我尝试通过IP地址访问时,它返回404页面。

我将 IP 与我的虚拟路径绑定,因为当我访问 http://192.168.1.2/ 时,它显示 http://www.example.com/admin/ 页面,即管理员的主页,没有任何问题。

以下链接失效并返回 404 页面 -

http://192.168.1.2/my/virtual/path

我猜它的 .htaccess 问题。这是我的 .htaccess 代码仅供参考

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ index.php [L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^([^?]*)$ %{ENV:BASE}index.php [NC,L]

这是我的 IP 虚拟主机配置 -

<VirtualHost xxx.xxx.xxx.xxx:80>
     DocumentRoot /path/of/my/hosting/location/public_html/admin
     <Directory "/path/of/my/hosting/location/public_html/admin">
         allow from all
         Options None
         Require all granted
     </Directory>
</VirtualHost>

这是我的域名托管部分 -

<VirtualHost example.com:80>
    DocumentRoot /path/of/my/hosting/location/public_html
    <Directory "/path/of/my/hosting/location/public_html">
        allow from all
        Options +FollowSymLinks
        allowoverride all
        Require all granted
        HostNameLookups on
    </Directory>
    ServerName example.com
    ServerAlias www.example.com
</VirtualHost>

【问题讨论】:

  • 我几乎可以肯定它与 vhost 而不是 mod_rewrite 有关。你的虚拟主机的内容是什么?
  • 它与虚拟主机无关,因为我可以通过192.168.1.2example.com 访问我的索引页面
  • 你查看服务器上的日志了吗?
  • 我之前忘记检查了,但我现在检查了。什么都没找到。没有写日志。只需访问发现 404 状态的日志
  • 愿意分享虚拟主机配置吗?

标签: php .htaccess mod-rewrite url-rewriting


【解决方案1】:

域的vhost部分内部有allowoverride all,它告诉apache允许更改.htaccess,但是ip的vhost部分没有。

allowoverride all 添加到ip vhost-block 应该没问题。

<VirtualHost xxx.xxx.xxx.xxx:80>
     DocumentRoot /path/of/my/hosting/location/public_html/admin
     <Directory "/path/of/my/hosting/location/public_html/admin">
         allowoverride all
         allow from all
         Options None
         Require all granted
     </Directory>
</VirtualHost>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-16
    • 2018-03-03
    • 1970-01-01
    • 2014-12-16
    • 2016-11-05
    • 1970-01-01
    • 2022-07-14
    • 2015-05-03
    相关资源
    最近更新 更多