简体   繁体   中英

Apache2 setting up mod_rewrite and restricting access without .htaccess file

I am following this tutorial:

http://www.phpro.org/tutorials/Model-View-Controller-MVC.html

The tutorial states you should use an htaccess file. However, the Apache2 documentation advises you to enter your .htaccess rules into the standard configuration files for better performance.

I'm working in /etc/apache2/sites-available/default .

This is what I have so far:

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order Deny,Allow
    Deny from all
</Directory>

<Directory /var/www/>
RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
</Directory>

<Location /index.php>
Order Allow,Deny
Allow from All
</Location>

With these rules, index.php is accessible, but index.php?rt=blog still works, and index/blog or /blog do not. What am I doing wrong?

but index.php?rt=blog still works

You don't have any rules that make it so that doesn't work

and index/blog or /blog do not

You don't have any rules that make URLs that look like that work, though "/blog" should be rewriting to /index.php?rt=/blog .

Try something like:

<Directory /var/www/>
    RewriteEngine On

    RewriteCond %{THE_REQUEST} \ /+index\.php?rt=([^&\ ]+)
    RewriteRule ^ /%1? [L,R]

    RewriteRule ^/index/(.*)$ /$1 [L,R]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/(.*)$ /index.php?rt=$1 [L,QSA]
</Directory>

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