2

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.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 1
    I am open to criticism. I would gladly read why my question is bad/unhelpful – Lajos Arpad May 12 '15 at 14:01
  • 1
    There is nothing you `need` to edit in vhost file. If the mod_rewrite module is enabled, then you just add your rules in the specific vhost in a `Directory ` directive. If it's not enable the module then add the rules as mentioned. – Panama Jack May 12 '15 at 14:05
  • I see. So, if it is enabled, things like this RewriteRule ^(.*)$ index.php [QSA,L] should work in a Directory tag? – Lajos Arpad May 12 '15 at 14:06
  • That's correct, there is nothing special you need to do. – Panama Jack May 12 '15 at 14:07
  • and Apache has to be restarted, right? – Lajos Arpad May 12 '15 at 14:07
  • That is correct. I could have just made an answer. but whatever. :) – Panama Jack May 12 '15 at 14:08
  • I will certainly accept the answer if it solves the problem. I am by far not an apache guru, so my question might be naive. This makes down-votes potentially valid. I would gladly sacrifice some karma to solve the problem and help future visitors sharing my confusion – Lajos Arpad May 12 '15 at 14:10
  • Well Jon, pretty much provided an answer on pretty much what I was saying. So it's fine. – Panama Jack May 12 '15 at 14:33
  • I ran a2enmod rewrite, restarted the server, the virtual host contains the content described by John Lin, but unfortunately the site does not work, index.php is not reached – Lajos Arpad May 12 '15 at 14:42
  • run `apache2ctl -M | grep rewrite` and say what the output is. – Panama Jack May 12 '15 at 14:46
  • It shows you the load modules and looks for rewrite. – Panama Jack May 12 '15 at 15:22
  • AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message rewrite_module (shared) – Lajos Arpad May 12 '15 at 15:24

1 Answers1

4

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.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • I am searching for httpd.conf. I hope I can find it easily – Lajos Arpad May 12 '15 at 14:29
  • Invalid command RewriteCond. I have to set the LoadModule, I guess. – Lajos Arpad May 12 '15 at 14:37
  • I ran a2enmod rewrite, restarted the server, but http://foo.com/bar is not running index.php – Lajos Arpad May 12 '15 at 14:44
  • @LajosArpad does `/bar` exist as a directory? – Jon Lin May 12 '15 at 14:49
  • No, it does not exist in the root. It exists in other places. But I would like to run index.php even if a bar directory does not exist. For instance foo.com/bar/lorem/ipsum should run index.php, which will parse the parameters, such that param0 = bar, param1 = lorem param2 = ipsum. Basically, I would like to run index.php regardless of the url. – Lajos Arpad May 12 '15 at 14:52
  • All this works locally, but there I allow .htaccess. However, I am a noob in apache and in regular expressions. – Lajos Arpad May 12 '15 at 14:53
  • I believe this can be achieved by a combo of rewrite conditions and rewrite rules, I just do not know how to write it. I have been reading about this for hours. However, as always, when someone is not proficient in an area, the results pour in very slowly. I am sorry for being a noob, I plan to learn about these aspects so in the future I will be able to give something back to the community for the favor I receive here. – Lajos Arpad May 12 '15 at 14:56
  • @LajosArpad Don't know why it's not working. [It works perfectly fine in my vhost config](http://i.stack.imgur.com/ZmFaX.png). Anything I request routes to the root's index.php – Jon Lin May 12 '15 at 15:13
  • I believe you, it is probably something to do with my configuration. I have edited my question and added vhost details. Can you take a look? Thank you again and sorry for the repeated bothering. I up-vote your answer anyway due to your attempt to try to help. – Lajos Arpad May 12 '15 at 15:18
  • @LajosArpad ok, is my bad, forgot to turn on the rewrite engine. In my vhost I had it turned on in another `` container – Jon Lin May 12 '15 at 15:28
  • Jon, this was the missing puzzle. I accept the solution as it is, since it solved the problem. Outside the question, if you would describe what these lines do, then I would understand the solution as well: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d Anyway, thank you very much for the solution. I have utmost respect for your nice attitude and the will to help. Well done, friend, kudos to you! – Lajos Arpad May 12 '15 at 15:32
  • Jon, the explanation is understandable, it allows me to understand what happens under the hood. Thank you very much. – Lajos Arpad May 12 '15 at 15:44