简体   繁体   English

没有.htaccess的Mod重写

[英]Mod rewrite without .htaccess

Let's suppose I have a website named 假设我有一个名为

foo.com

I can access foo.com and it runs the index.php found in the root folder. 我可以访问foo.com ,它运行在根文件夹中找到的index.php My question is: how should I edit the vhost file to enable rewrite mod without enabling htaccess? 我的问题是:如何在不启用htaccess的情况下编辑虚拟主机文件以启用重写mod?

My goal is to be able to write 我的目标是能够写

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

into my browser address bar and to run index.php regardless of the number of / characters in the url. 进入我的浏览器地址栏,并运行index.php,而不管URL中的/字符数如何。 My index.php would handle the parameters separated by / 我的index.php将处理用/分隔的参数

How can I achieve this? 我该如何实现?

EDIT: vhost file: 编辑:虚拟主机文件:

<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>

EDIT: The problem, as the accepted answer suggests is that this host did not contain the line which turns the rewrite engine on. 编辑:问题,正如公认的答案所暗示的是,该主机不包含打开重写引擎的行。 This line is present in the answer. 该行出现在答案中。 Which solves the problem. 解决了问题。

To disallow the use of htaccess, you need this directive in a <Directory> container for your document root, then you can just place mod_rewrite rules in the same container: 要禁止使用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>

assuming that "/var/www/htdocs" is you document root. 假设“ / var / www / htdocs”是您的文档根目录。

To ensure mod_rewrite is loaded, check the httpd.conf file for a LoadModule line that contains mod_rewrite, and make sure it's uncommented. 为了确保已加载mod_rewrite,请检查httpd.conf文件中是否包含包含mod_rewrite的LoadModule行,并确保未对其进行注释。 You'll need to restart your server anytime you make changes to the vhost config. 更改vhost配置后,您将需要重新启动服务器。

Short explanation: 简短说明:

The lines: 这些行:

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

are conditions to check that the request is not an existing file ( -f ) or an existing directory ( -d ). 是检查请求是否不是现有文件( -f )或现有目录( -d )的条件。 These conditions serve 2 main purposes: 这些条件有两个主要目的:

  1. It prevents the rewrite engine from looping, so that index.php won't also get rewritten. 它防止了重写引擎的循环,因此index.php也不会被重写。 Since index.php is an existing file, the conditions stop the rewrite engine. 由于index.php是现有文件,因此条件将停止重写引擎。
  2. It allows resources and assets like images or scripts from being rewritten. 它允许重写图像和脚本之类的资源和资产。

If you want everything routed to index.php no matter what (including images or anything else), then you can change the 2 conditions to simply: 如果您希望所有内容(包括图像或其他内容) 路由到index.php ,则可以将2个条件更改为:

RewriteCond %{REQUEST_URI} !^/index.php

so that everything gets rewritten except index.php . 这样,除index.php之外的所有内容都将被重写。

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

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