Apache mod rewrite not working on mac os x lion - php

I'm trying to set up mod rewrite on os x and it doesn't seem to be working properly.
I've edited my httpd.conf file to include the module, I've changed the AllowOverride setting to "all" in both my httpd.conf and my user.conf file.
Now when I add a htaccess file with the following:
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
I get a 404 not found saying ...
"The requested URL /Users/matt/Sites/framework/public/ was not found on this server."
Even though that folder does exist.
The fact that it's redirecting would suggest that mod rewrite is installed and working, but I'm not sure why I'm getting a 404. Any one else had this problem?
Thanks

I did test your rewrite rule on a Windows machine and it thrown Internal server error. I tweaked it a little bit and came to this solution and it seems to work just fine on my test environment.
Could you give it a try and see if it's ok for you?
RewriteEngine on
RewriteRule ^public/ - [L]
RewriteRule .* public/$0 [L]

Related

.htaccess mod rewrite and MAMP

having issues with what i think is linked to the mod rewrite in mamp.
The rewrite works fine on the live server, but im working locally with MAMP and getting issues.
The problem is linked to this:
I rewrite the following URL:
http://localhost/BuildSanctuary-Dev/viewbuild/64/three-fiddy-z/1
That should rewrite as:
http://localhost/BuildSanctuary-Dev/viewbuild.php?id=64&title=three-fiddy-z&page=1
The issue is that i get a 404 for page not found.
The requested URL /viewbuild.php was not found on this server.
Which it clearly is... but if i visit just /viewbuild.php it works fine.
The rewrite in the .htaccess is:
RewriteRule ^viewbuild/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /viewbuild.php?id=$1&title=$2&page=$3 [L,QSA]
Any ideas? I have done the research and everything in the apache conf is showing as Allowing All...
Thanks.
Your rewriterule redirects to /viewbuild.php and not /BuildSanctuary-Dev/viewbuild.php:
RewriteRule ^viewbuild/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /BuildSanctuary-Dev/viewbuild.php?id=$1&title=$2&page=$3 [L,QSA]
A mabye better variant would be to put the .htaccess into the /BuildSanctuary-Dev/ folder and use a relative path as redirect:
RewriteBase /BuildSanctuary-Dev
RewriteRule ^viewbuild/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ viewbuild.php?id=$1&title=$2&page=$3 [L,QSA]

.htaccess RewriteRule conflict across different directories

On my Ubuntu development machine(s) I run a LAMP stack. For each website I work on, I create a new directory off root:
/var/www/somesite.com
/var/www/anothersite.com
The problem I have is that apache wont allow duplicate rewrite rules across these folders. For instance, If I set up this:
RewriteRule ^track/(.*)$ /somesite.com/order_track.php [nc,L]
http://localhost/somesite.com/track/abc123 - works as intented
This same declaration wont work on anothersite.com
RewriteRule ^track/(.*)$ /anothersite.com/order_track.php [nc,L]
http://localhost/anothersite.com/track/abc123 - Apache returns a 404.
Clearing browser cache and restarting Apache have no effect. Apache seems to "remember" the first like rewriterule used. This happens on all of my computers(Home, work, laptop)
Edit: I should have mentioned that I have an htaccess file in each directory. The root /var/www does not contain an htaccess file. Each directory's htaccess should operate independently. But they do not.
You can have this in /somesite.com/.htaccess:
RewriteEngine On
RewriteBase /somesite.com/
RewriteRule ^track/(.*)$ order_track.php [NC,L]
Then this in /anothersite.com/.htaccess:
RewriteEngine On
RewriteBase /anothersite.com/
RewriteRule ^track/(.*)$ order_track.php [NC,L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(somesite\.com|anothersite\.com)$
RewriteRule ^track/ /%1/order_track.php [NC,L]
you don't need (.*) if not needed to catch this.

Very simple .htaccess mod_rewrite configuration gives 404

Ok, I'm starting to think that the problem is me but... ¿What's wrong here?
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule testpage\.html http://www.google.com [R] #It works
RewriteRule ^mineral/([0-9]+)/?$ ver.php?id=$1 [NC,L] #It doesn't
I have a folder called "WebX" and this .htaccess inside it along with other files from the web.
mod_rewrite is working ok according to phpinfo and everything is running on localhost.
The most courious thing is that if I type localhost/WebX/testpage.html it redirects to Google
but unfortunately for me if I type localhost/WebX/mineral/1 it gives me 404. ¿What happens?
The problem you are having is caused by RewriteBase /. Your .htaccess is in a subdirectory /WebX/, but you are telling mod_rewrite to rewrite rules as if the current directory was /. It tries to load a file ver.php in your www-root, which doesn't exist. If you have verbose error messages with what file it tried to load, you'll notice it says that it tried to load /ver.php and not /WebX/ver.php as you might expect.
The rest you are doing, most notably using a relative url instead of a / prefixed absolute url, seems correct. The correct .htaccess to use would be:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /WebX/
RewriteRule testpage\.html http://www.google.com [R] #It works
RewriteRule ^mineral/([0-9]+)/?$ ver.php?id=$1 [NC,L] #It now should work

Apache rewrite rule works on localhost but not on server

I have a site that I would like to use Apache's RewriteRule to rewrite URLs.
I want:
http://baileyseymour.com/index.php?p=home
to rewrite to
http://baileyseymour.com/p/home
I have AMPPS installed on my Mac and I added the following lines to httpd.conf and they work successfully:
RewriteEngine On
RewriteRule ^/p/(.*) /index.php?p=$1 [PT]
I'm trying to do the same but on my server.
And I have added the same apache code to /public_html/.htaccess but I get the error message below:
Not Found
The requested URL /p/home was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
The exact same code works on my localhost server. Why not on my website?
Can you check your remote server apache supports "AllowOverride All" ?
also try this way maybe it will help.
RewriteEngine On
RewriteRule ^p/(.*) /index.php?p=$1 [PT]
but you may have to modify $_GET['p'] properly. which will be sent only "home" part.
You need to remove the leading slash from the rewrite rule's pattern. URI's have their leading slash removed when the rewrite engine applies rules in an htaccess file.
RewriteEngine On
RewriteRule ^p/(.*) /index.php?p=$1 [PT]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.baileyseymour\.com
RewriteRule ^(.*)$ http://www.baileyseymour.com/$1 [R=301,L]
</IfModule>
.htaccess is the right place to put them and that file should be in the same directory as your default home page.

Removing the index.php file of codeigniter

http://codeigniter.com/user_guide/general/urls.html
I am using a MAMP setup
I access my codeigniter welcome message as follows
http://localhost:8888/CodeIgniter_2.0.2/index.php/welcome
the tutorial says
use a .htaccess file as
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
to get same message with
http://localhost:8888/CodeIgniter_2.0.2/welcome
but I get a 404 not found error instead
Try to add RewriteBase on your htacces file.
RewriteEngine On
RewriteBase "/CodeIgniter_2.0.2/"
...your rules here...
the last line should be
RewriteRule ^(.*)$ index.php/$1 [L]
and not
RewriteRule ^(.*)$ /index.php/$1 [L]
First things first, you need to check your httpd.conf
Look for this line.. (the location may not be the same)
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Make sure it's uncommented. Then save your changes and restart apache server.
Also, make sure that the .htaccess file is in the same directory as your index.php.
It's usually the base directory of your CodeIgniter application.
EDIT:
Also forgot to mention that you need virtual hosts for this work seamlessly.
Argh, it seems your question is a repost. Here's another question but this is on a MAC OS X platform, with some detail on setting up your virtual hosts.
Remove "index.php" from URL - Codeigniter
the instructions you followed assume you are installing code igniter in the docroot, and not a subdirectory. move all your files in /CodeIgniter_2.0.2 down a directory.
mv CodeIgniter_2.0.2/* .

Categories