繁体   English   中英

没有.htaccess的Mod重写

[英]Mod rewrite without .htaccess

假设我有一个名为

foo.com

我可以访问foo.com ,它运行在根文件夹中找到的index.php 我的问题是:如何在不启用htaccess的情况下编辑虚拟主机文件以启用重写mod?

我的目标是能够写

http://foo.com/bar/loremipsum/dolor

进入我的浏览器地址栏,并运行index.php,而不管URL中的/字符数如何。 我的index.php将处理用/分隔的参数

我该如何实现?

编辑:虚拟主机文件:

<VirtualHost *:80>
        ServerName myproject.com
        ServerAlias www.myproject.com

        ServerAdmin webmaster@localhost
        DocumentRoot /opt/apps/myproject

        <Directory /opt/apps/myproject>
            # disable htaccess
            AllowOverride None

            # route everything to index.php
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^ /index.php [L]

            Require all granted
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

编辑:问题,正如公认的答案所暗示的是,该主机不包含打开重写引擎的行。 该行出现在答案中。 解决了问题。

要禁止使用htaccess,您需要在<Directory>容器中将此指令用于文档根目录,然后可以将mod_rewrite规则放在同一容器中:

<Directory "/var/www/htdocs/">
    # disable htaccess
    AllowOverride None

    # route everything to index.php
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ /index.php [L]
</Directory>

假设“ / var / www / htdocs”是您的文档根目录。

为了确保已加载mod_rewrite,请检查httpd.conf文件中是否包含包含mod_rewrite的LoadModule行,并确保未对其进行注释。 更改vhost配置后,您将需要重新启动服务器。

简短说明:

这些行:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

是检查请求是否不是现有文件( -f )或现有目录( -d )的条件。 这些条件有两个主要目的:

  1. 它防止了重写引擎的循环,因此index.php也不会被重写。 由于index.php是现有文件,因此条件将停止重写引擎。
  2. 它允许重写图像和脚本之类的资源和资产。

如果您希望所有内容(包括图像或其他内容) 路由到index.php ,则可以将2个条件更改为:

RewriteCond %{REQUEST_URI} !^/index.php

这样,除index.php之外的所有内容都将被重写。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM