问题
I have a website project on my mac book pro leopard, and I am using built in apache2 and php. I've made my configurations in both httpd.conf and user.conf to use htaccess. Changes done like :
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
<Directory >
AllowOverride All
</Directory>
The problem is when i want to open a site like localhost/~username/site/site/index.php/welcome, index.php does some operations and finds the right controller and the right page.
But when I try to enter site like site/welcome apache gives me the following error :
Not Found
The requested URL Users/username/Sites/site/index.php/welcome was not found on this server.
The problem is apache gets this like it is indeed a file and the error statement gives it in a file system way, but this request must be fetched to index.php instead.
my .htaccess file looks like this
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
</ifModule>
What bothers me that this configuration works on both Linux and Windows but does not work on Mac. I feel doomed : )
回答1:
I met the same problem too! 'DocumentRoot' doesn't work for me in Lion.
But this works:
RewriteEngine On
RewriteBase /~username/YOURPath
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
回答2:
On Mac OSX Mountain Lion, make sure AllowOverride
is set to All
on Directory /
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
and root directory of your document root path:
<Directory "/Library/WebServer/Documents">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
also make sure that FollowSymLinks
is included in Options
.
if your "index.php-less pretty URL" is not on your DocumentRoot then add the following RewriteBase directory on your .htaccess file where your index.php exists, like the following example:
RewriteBase /~username/site/site/
If you would like to change the .htaccess
file name to something else like .acl
or htaccess.txt
, uncomment the following line on your httpd.conf
# Various default settings
Include /private/etc/apache2/extra/httpd-default.conf
and change .htaccess
in AccessFileName
directive to anything you like in extra/httpd-default.conf
file. For example if you would like to change .htaccess
file to htaccess.txt
change it to:
AccessFileName htaccess.txt
save them, restart your apache web server on OSX Mountain Lion, and hope you are good to go. Hope this helps.
回答3:
You need to add the following line to your .htaccess file:
DocumentRoot "/~username/site/"
回答4:
I just had the same problem but the solutions above did not work for me,
Removing the slash from the beginning did the trick (i guess because my browser already runs under the correct user).
So RewriteRule ^Register /register.php
Became RewriteRule ^Register register.php
That drove me nuts! BTW I am running xampp (apache 2) on mac os 10.8 .
回答5:
I had the same issue but for me the problem was that mod_rewrite
was not enabled.
I had to uncomment these two line in the /etc/apache2/httpd.conf file:
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php7_module libexec/apache2/libphp7.so
After that there was no need to use RewriteBase
etc.
回答6:
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".
来源:https://stackoverflow.com/questions/7941958/htaccess-rewriterule-not-working-correct-on-mac