RewriteRule in .htaccess not working

别来无恙 提交于 2019-12-19 07:49:38

问题


I am currently running Apache2 on my local machine, installed with the latest version of Ubuntu.

I am trying to get basic URL rewriting working by using the .htaccess file.

The file "http://localhost/page.php?=home" does exist, and the location "/doesnotexist/home" does not.

I would like to have the first page be loaded when the second is requested.

My .htaccess file looks like this:

RewriteEngine On
RewriteRule ^/doesnotexist/(.*)$ /page.php?p=$1 

My httpd.conf file looks like this:

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

<Directory /var/www>
    AllowOverride All
</Directory>

Note that my httpd.conf file looks exactly like that, as it was empty before I edited it.

The result that I get is this:

Not Found

The requested URL /doesnotexist/home was not found on this server.

I have googled the ever living **** out of this problem, and I have never gotten anything other than the error above.

If anyone has any ideas, I would be very appreciative.


回答1:


For the benefit of others, I figured out the answer:

In the file "/etc/apache2/sites-enabled/000-default" there was the line:

AllowOverride None

Change this to:

AllowOverride All



回答2:


You need to remove the contextual path prefix from your pattern when using mod_rewrite in a .htaccess file. In the case of the root directory, the path prefix is just /. So try this:

RewriteRule ^doesnotexist/(.*)$ /page.php?p=$1



回答3:


If I place a .htaccess into /Library/WebServer/Documents and open "localhost/"; to test it, this works as expected. It just doesn't work in "~/Sites". I have tried this on Mac OS X Mavericks.



来源:https://stackoverflow.com/questions/2194137/rewriterule-in-htaccess-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!