ricardo
ricardo

Reputation: 1581

apache not working with index on RewriteEngine on Mac Lion

I have a strange problem that only happen on My mac(I do not try in another mac, but did tried on a linux and it works)

this is my .htaccess

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?key=$1

on my index.php I put:

echo $_GET['url'];

it is return a empty array if my url is like this:

localhost/test/index/xpto

but it is return 'index2/xpto' if I put this in the url:

localhost/test/index2/xpto

So the problem is with the string index.

Do you guys know why it do not work on my mac but it does on linux?

I am using standard apache on mac os x lion. I just change this lines on httpd.conf

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Deny from all
</Directory>
....
<Directory "/Library/WebServer/Documents">
    AllowOverride All
    ...

LoadModule rewrite_module libexec/apache2/mod_rewrite.so

Edited

By working I mean the $_GET['key'] is not empty. And shows the url I put in the browser.

but when I use this url on browser:

http://127.0.0.1/test/index/xpto

my $_GET['key'] is empty

and if I use this url:

http://127.0.0.1/test/anythingbutindex/xpto

the $_GET['key'] have the text: anythingbutindex/xpto

I did install XAMPP on my mac and this code works fine with index. So the problem is with the default apache on my MAC.

thanks.

Upvotes: 0

Views: 457

Answers (2)

Dimitre L
Dimitre L

Reputation: 3

I've had this issue right now and the easiest way to make it work was reinstalling apache via homebrew, so it works identical to the Linux I'm using on a VPS. It took about two minutes to reinstall, reconfigure the port and I've used virutalhost.sh script to point to the ~/Sites folder

Upvotes: 0

Qben
Qben

Reputation: 2623

I don't understand how this is working on Linux, or what result that is "working". To me it looks like it's doing what one could expect. I however think your RewriteRule might be a bit wrong. What it will do is that it will pass the entire URI to the index.php file, while I think you just want to parameters after index.php?

If you enter localhost/test/index/xpto, and assuming your DocumentRoot is set to /test the string sent to index.php will be index.php/exto. Same thing apply to index2/exto since the RewriteRule does not care about if you enter index och index2.

You could try this:

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^index/(.*)$ index.php?key=$1

If you do want the index parameter in there you could change the RewriteRule to:

RewriteRule ^(index/)(.*)$ index.php?key=$1$2

Hope it helps you forward.

EDIT

After the clarification I understand the question. Some testing later I found that the solution was pretty obvious, but I did not spot it at first. The RewriteRule is slightly off and if I use this it's almost working on my system.

RewriteRule ^(.*)$ /index.php?key=$1 [L]

Note the / before index.php. However this would always redirect to index.php meaning always match the RewriteCond statements. Changing the statement to this however worked at my system.

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d

If you have DirectoryIndex set the !-d is kind of redundant since it will always try <path>/index.html or whatever you have configured.

Upvotes: 1

Related Questions