apache not working with index on RewriteEngine on Mac Lion - macos

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.

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.

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

Related

Why Rewriterule doens't work in my .htaccess file?

I've been struggling with this very rare problem.
I had the following code in my .htaccess file:
Order deny,allow
DirectoryIndex index.php
RewriteEngine on
Rewriterule ^producto/(.+)/(.+) producto.php?id=$1&title=$2
Rewriterule ^pedidos/ pedidos.php
Rewriterule ^peliculas/(.+)/(.+) listado.php?tipo=1&gen=$1&pag=$2
Rewriterule ^musicales/(.+)/(.+) listado.php?tipo=2&gen=$1&pag=$2
Rewriterule ^blueray/(.+)/(.+) listado.php?tipo=4&gen=$1&pag=$2
Rewriterule ^condicionadas/(.+)/(.+) listado.php?tipo=5&gen=$1&pag=$2
Rewriterule ^series/(.+)/(.+) listado.php?tipo=3&gen=$1&pag=$2
And everything worked fine. When I wanted to change the fifth line:
Rewriterule ^pedidos/ pedidos.php
to this:
Rewriterule ^pedidos/(.+) pedidos.php?estado=$1
It did not work. No matter how much I change that line, it won't work.
BUT if I change any other line, the change works.
It's like there's a cached file but I restarted WAMP, I cleaned my computer and browsers with CCleaner, and nothing works.
Can you help me with this?
Try to add that before RewriteEngine on:
Options -MultiViews

CodeIgniter and specific rewrite rule

On my CodeIgniter site, I would like to add a specific rewrite rule, so that this url
http://www.exemple.com/cache.manifest
would rewrite to
http://www.exemple.com/controller/manifest
(because Safari 7 seems to only accept .manifest files for ApplicationCache)
So I try to add this line to my htaccess
RewriteRule ^cache.manifest$ controller/manifest
I added it before the other rewrite rules :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^cache.manifest$ controller/manifest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
But it returns a 404. If I change the line to
RewriteRule ^cache.manifest$ test.html
it works. So the first part on my rule is correct.
If I try to access directly to www.example.com/controller/manifest, it works to, so my url is correct.
I tried also
RewriteRule ^cache.manifest$ index.php/controller/manifest [L]
But it doesn't work either…
Any clue ?
Thanks a lot
I tried some tests on my local server and I think the following might work:
RewriteRule ^cache.manifest$ /index.php/controller/manifest [R,L]
I am not entirely sure if you need the leading "/" or "/index.php", so you
may need to experiment.
You need the [R] flag to force a redirect. In this situation, you want Apache
to look for the string cache.manifest in the URL, and then go to the CI page
controller/manifest.
It appears that you need to explicitly set the redirect.
Please let me know if this works. Good luck!

htacces mac os not working with pseudo directory

i managed to make htaccess working on mac os x but the subcateg rule returns a file not found (404) as i guess does search from the articles folder(?)
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /ks
RewriteRule ^articles/(.*).html$ articles.cfm?subcateg=$1 [NC,L]
RewriteRule ^articles.html$ articles.cfm [NC,L]
The second one seems to be working fine...
Looking at your code, your RewriteBase is /ks/ folder, which means you access it with domain.com/ks/articles/xxxxx.html or domain.com/ks/articles.html and your htaccess is in /ks/ folder.
If that's right, you should disable MultiViews option to avoid your problem.
This code should work on osx, too
Options +SymLinksIfOwnerMatch -MultiViews
RewriteEngine On
RewriteBase /ks/
RewriteRule ^articles/(.+?)\.html$ articles.cfm?subcateg=$1 [NC,L]
RewriteRule ^articles\.html$ articles.cfm [NC,L]

Yii :: Issue Using Showscriptname = False And Url Manager

I am currently having an issue using the URL manager and/or the apache mod_rewrite or maybe something else entirely.
With showScriptName set to false, navigating to addresses such as domain.com/login, domain.com/logout, domain.com/site/login are all behaving the same way. It simply shows the main site/index, as if I were to navigate to domain.com/eeofjew9j8jfdedfmewf (jibberish).
Maybe it's an issue with my Yii settings? Here are those (sorry for the sloppiness):
'components'=>array(
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'login'=>'site/login',
'logout'=>'site/logout',
'register'=>'users/register'
),
,...
Here is how I have my .htaccess setup in the www root:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
I am using a VPS server, so I have root access to make any changes needed to apache. I've checked my phpinfo and mod_rewrite is running and enabled. In my apache config, I have for my www directory:
AllowOverride All
Options Indexes FollowSymLinks MultiViews
Order allow,deny
Allow from all
I've been scratching my head on this issue through 3 different hosts (godaddy, dreamhost, and lithiumhosting) so I'm assuming it's an issue on my end with this. Now that I have a VPS though, I'm hoping I can finally figure out my issue and solve it.
First verify that the server is parsing the .htaccess file. This is simply done by placing random text in the .htaccess file such as
CREATE AN ERROR
If this gives you a server error then the file is being parsed. You may have to move the .htaccess file up a directory to get this to work.
After this is working check to make sure the rewrite module in Apache is on. First check phpinfo and look for the running module. If not you may have to delete the comment (#) character in front of the module.
After the module shows up in phpinfo then see if you can do a basic rewrite to make sure that there are not problems with Apache.
RewriteEngine on
RewriteRule ^ http://google.com/? [L,R]
If that is not working try adding the
Options +SymLinksIfOwnerMatch
to the file
Once you have Apache doing it's part now it is up to yii. This code:
RewriteEngine On
RewriteBase /mybasedirectory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
adds the rewrite base option which may be necessary if you do not have the files in the document root such as htdocs
finally if that does not solve the problem then limit your rule to a simple rule such as
'contact'=>'site/contact',
and see if at least redirects to where you think it should. Of course you may have to check the basic rules of .htaccess again to make sure that overide is allowed and there isn't an errant .htaccess in a subdirectory. I hope that helps. I finally got mine working.
What happens if you delete these lines!
enter code here
# otherwise forward it to index.php
RewriteRule . index.php
if you add this code see if it works
'<action:(login|logout|register|contact)>' => 'site/<action>',

Clean URLs on OS X with MAMP using htaccess

Last weekend I set up a new site at home on Windows (with xampp). Everything works, I get clean URLs (like http://mysite.com/page/about). Today I had to bring the files to my office where I'm using a Mac with MAMP and nothing worked. I change AllowOverride to All in the /Applications/MAMP/conf/apache/httpd.conf file, and enabled LoadModule rewrite_module modules/mod_rewrite.so, with no luck. (Of course, I restarted MAMP)
But nothing happend. All of my webrowsers tell me nothing about the error. Just got the 'localhost:8888/' at the title.
I use these lines in .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9_-]+)(/)?$ index.php?page=$1 [QSA]
RewriteRule ^page/([A-Za-z0-9_-]+)(/)?$ index.php?page=page&uri=$1
RewriteRule ^work/([A-Za-z0-9_-]+)(/)?$ index.php?page=work&uri=$1
</IfModule>
What else I could do to make this working?
Thanks a lot.
This allowed me to serve the file about.php in response to the request path /about:
RewriteRule ^([A-Za-z0-9_-]+)(/)?$ /$1.php [L]
Note that on MAMP you have to put this in .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9_-]+)(/)?$ /$1.php [L]
</IfModule>
Putting that same block in httpd.conf did not work for me on MAMP.

Resources