1

I've been reading up on this on the web, but still could not figure out how to implement it properly. I'd greatly appreciate if you could help me understand how to make url rewrite work without .htaccess.

To check if mod_rewrite is enabled, I ran command ~# sudo apachectl -t -D DUMP_MODULES. It produced, among others the following module: rewrite_module (shared). I don't know if it is the same as mod_rewrite?

Folder /etc/apache2/mods-enabled/ has file rewrite.load

I'm not clear which file exactly I should add rewrite rules to?

File httpd.conf located in /etc/apache2/ is empty. However there's a file named 000-default located in /etc/apache2/sites-enabled/ and it looks like this:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

I'm not clear at all where I should add rewrite rules. I tried adding them to this 000-default file into <Directory /var/www/> section, but it did not work.

2 Answers 2

1

You put the rewrite code in the directory block where you're have the .htaccess file (if you use them). For example if /var/www is your docroot, you could put it their:

    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all

        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule . /index.php [L]
    </Directory>
Sign up to request clarification or add additional context in comments.

8 Comments

It's not working for me for some reason. What I'm trying to do is to remove .php extenstion in the url. As you instructed I have added the rewrite rules (that work in .htaccess) to <Directory /var/www/> of 000-default file, so it now look like this, but does not work: <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L]</Directory>
@dmit this worked in your .htaccesss file? What directory was the .htaccess file in?
Also can you provide in your OP a before and after URL example?
I have it in Root directory>httpdocs. I moved it Root directory and it works as well. Example, before: www.mysite.com/login.php; after: www.mysite.com/login
I'm not understanding. Do you mean your code and .htaccess file is in /var/www/httpdocs ?
|
1

If you are using apache 2.2.16 or newer, use

FallbackResource /index.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.