简体   繁体   中英

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. My question is: how should I edit the vhost file to enable rewrite mod without enabling htaccess?

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. My index.php would handle the parameters separated by /

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:

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

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. You'll need to restart your server anytime you make changes to the vhost config.

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 ). These conditions serve 2 main purposes:

  1. It prevents the rewrite engine from looping, so that index.php won't also get rewritten. Since index.php is an existing file, the conditions stop the rewrite engine.
  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:

RewriteCond %{REQUEST_URI} !^/index.php

so that everything gets rewritten except index.php .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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