How to activate mod_rewrite?

前端 未结 6 1335
小蘑菇
小蘑菇 2020-12-16 19:17

I know that this question have been asked several times. But I can\'t get it to work.

I installed Apache2 in my Ubuntu server I can also confirm that mod_rewrite is

相关标签:
6条回答
  • Please check /etc/apache2/apache2.conf

    0 讨论(0)
  • 2020-12-16 19:53

    Because this thread ranks very high on google, here another solution:

    WHAT'S CAUSING THE PROBLEM ?

    It's caused because the standard definition in apache simply blocks all .htaccess stuff. Seriouslsy. This seems to be the initial state in Ubuntu 12.04 LTS.

    SOLUTION

    In /etc/apache2/sites-available/default (that's a file, not a folder) there's a code block like

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

    Here you should change

    AllowOverride None
    

    to

    AllowOverride All
    

    IMPORTANT

    You have to do these changes with sudo rights, anotherwise Ubuntu will not save the file. After a RESTART your .htaccess stuff should work. To test .htaccess simply put some nonsense text into it and surf to localhost or 127.0.0.1 -> If you see "internal server error": voila! Remove the nonsense and you are ready to go.

    0 讨论(0)
  • 2020-12-16 20:03

    All suggestions from Niels Bom are the smartest ones (I think). But I would add this as the first suggestion: try to launch and stop apache via the Ubuntu command: then when it's supposed to be stopped, make sure it's stopped ie verify your local page doesn't show anymore.

    • read what's in the folder /etc/apache2/. There should be apache2.conf conf.d envvars httpd.conf mods-available mods-enabled ports.conf sites-available sites-enabled
    • then if everything is ok, take a look at mods-enabled where you will find if the mods are enabled if so, then you can go on, otherwise take a look at the other answers about enabling modrewrite
    • take a look at sites-enabled: it's where you can find the sites that are... (guess what?) enabled. There should be the default website.
    • if so, open this file, and try to put apache nonsense in it, restart the server and see if it's happy or not. If it's not happy that's good. If it's happy then you're working on the wrong file
    • last: try to check what are the authorisations for htaccess files for this default vhost. Here's the way they work: if Apache doesn't find any directive in the vhost file, it will apply the global configuration. So I'd suggest to add the configuration in your vhost file, between the <virtualhost> - </virtualhost> tags (of course).

    Please tell me what's going on and I'd be glad to update my question accordingly.

    0 讨论(0)
  • 2020-12-16 20:10

    Okay, let's check assumptions:

    1. Put nonsense in your Apache config files (and restart Apache to let them take effect), does Apache "complain" on restart? Then it tries to load those files. Try this for every Apache config file you can find. You now have a complete list of Apache config files that are loaded.

    2. put nonsense in your htaccess, reload the page, do you get a 500 error? If not, the htaccess is not getting loaded.

    3. Put the most basic mod_rewrite statement in your htaccess:

      Options +FollowSymLinks RewriteEngine On RewriteRule ^.*$ test.html

    This should rewrite every request to test.html.

    Try this and give us the results.

    0 讨论(0)
  • 2020-12-16 20:12

    if you run this command,

    sudo a2enmod rewrite
    

    ubuntu will output whethere it is activated or already running.

    0 讨论(0)
  • 2020-12-16 20:13

    In your question, you have mentioned,

    Also, my apache2.conf file doesn't work. I tried to write som nonsense. And it is still gives me the normal result instead of http 500 error

    So, before posting more stuff to try-out with .conf files,

    run strace -o somefilename.txt sudo /etc/init.d/apache restart

    strace - trace system calls and signals : strace(1) - Linux man page.


    The -o somefilename.txt part puts the output of strace into somefilename.txt. Open the text file and search for httpd.conf. You should be able to see code similar to:

    stat("/etc/httpd/conf/httpd.conf", {st_mode=S_IFREG|0644, st_size=35894, ...})=0
    open("/etc/httpd/conf/httpd.conf", O_RDONLY|O_CLOEXEC) = 3
    

    The path to httpd.conf will be replaced by the path to your default httpd.conf

    If you find one, then open that httpd.conf and make changes there. Else, a httpd.conf is not getting loaded.

    If later is the case, try:

    sudo /etc/init.d/apache -f  /path/to/httpd.conf start
    

    Also, check for NameVirtualHost *:80 and make sure it is not commented.

    0 讨论(0)
提交回复
热议问题