3 Replies - 211 Views - Last Post: 07 February 2012 - 07:30 PM Rate Topic: -----

Topic Sponsor:

#1 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 118
  • View blog
  • Posts: 1,257
  • Joined: 01-June 09

Apache mod_rewrite not rewriting

Posted 07 February 2012 - 07:12 AM

Not sure why this is not working. I've checked in httpd.conf and I have this line:
LoadModule rewrite_module modules/mod_rewrite.so

And when I do phpinfo(), I see mod_rewrite in the loaded modules box.

My directory structure: I'm using Apache, so there's a htdocs directory. Inside the htdocs directory I have myProject directory. This is the root of my site. Within the my Project directory I have all the files and folders that make up my site. Sitting right in this "root" myProject directory I created a .htaccess file (making sure that it doesn't have a .txt type).

Here's the contents of this file:
RewriteEngine On


RewriteBase /
    
RewriteRule .* index2.php



And here's index2.php (which is located right in myProject):
<?php
echo "hiiii";
class Foo {
	static public function test($classname)
	{
	    if(preg_match('/\\\\/', $classname))
		{
			$path = str_replace('\\', '/', $classname);
		}
		else
		{
			$path = str_replace('_', '/', $classname);
		}
		
		if (is_readable($path.".php"))
		{
		    require_once $path .".php";
		}
	}
}
spl_autoload_register('\Foo::test');

\controller\Controller::run();
?>



So no matter what I do, this index2.php file should be run, right? Well it's not. I know it's not because I'm getting no echo of hiiii unless I physically go to index2.php.

What could be going wrong?

Thanks!

EDIT:
Just want to make a note that I believe the problem is that the .htaccess file is not being read at all. Here's why I think that:

I updated the .htaccess file to say this:
RewriteEngine On


RewriteBase /
This is garbage
RewriteRule /.* index2.php



This should throw a 500 internal server error, should it not? Well it doesn't. When I go to this url:
http://localhost/myProject/i

I am just getting a 404 Not Found Error screen.

This post has been edited by eZACKe: 07 February 2012 - 07:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Apache mod_rewrite not rewriting

#2 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 118
  • View blog
  • Posts: 1,257
  • Joined: 01-June 09

Re: Apache mod_rewrite not rewriting

Posted 07 February 2012 - 10:15 AM

Well I got it figured out, the problem was in my httpd.conf it had AllowOverride: None. I simply switched it to All, restarted Apache, and that fixed it.

Now I'm having a bit of a bigger problem though, and I'm not sure if it has to do wit my RewriteRule or RewriteCondition.

This is my .htaccess file:
RewriteEngine On
Options FollowSymLinks
	
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|png|gif|swf)$ [NC]
RewriteRule ^([^/]+)/?([^/]+)?/?([^/]+)?/? index2.php?cmd=$1&action=$2&ajax=$3 [L,QSA]



What I want it to do is take urls like this:
localhost/myProject/yes/no/maybe

And rewrite it to:
localhost/myProject/index2.php?cmd=yes&action=no&ajax=maybe

Also though, I want each one to be optional.

Like it should allow localhost/myProject/forums and just turn it to
localhost/myProject/index2.php?cmd=forums&action=&ajax=

Or localhost/myProject/ would just go to localhost/myProject/index2.php

Here's the problem I'm having:

This works fine:
localhost/myProject/yes

However, anything with more than that does not work.
Like for example
localhost/myProject/yes/
localhost/myProject/yes/no
localhost/myProject/yes/maybe

All don't work.

The reason they aren't working. It's changing the url for my files in the head of my html file.

In my head I have this for a css file src:
common/css/redesign_css.css

When I use this url, it's fine: localhost/myProject/yes

When I use this url:
localhost/myProject/yes/

The full path to the css file becomes: localhost/myProject/yes/common/css/redesign_css.css


When I use this url:
localhost/myProject/yes/no/

The full path to the css file becomes:
localhost/myProject/yes/no/common/css/redesign_css.css


Why is it changing the absolute paths?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#3 AdaHacker  Icon User is online

  • Resident Curmudgeon

Reputation: 377
  • View blog
  • Posts: 739
  • Joined: 17-June 08

Re: Apache mod_rewrite not rewriting

Posted 07 February 2012 - 11:29 AM

View PosteZACKe, on 07 February 2012 - 12:15 PM, said:

In my head I have this for a css file src:
common/css/redesign_css.css
...
The full path to the css file becomes: localhost/myProject/yes/common/css/redesign_css.css
...
Why is it changing the absolute paths?

So, just to be clear, you're using a relative path for the CSS file, right? If that's the case, then nothing is changing any absolute paths. A relative URL for a linked file is relative to the current URL. So the browser is going to treat the last "directory" component of the URL as the base and request linked files relative to that. If the URL you request is http://localhost/myProject/yes/, that URL looks like a directory and so will be used as the base for relative URLs. So, in other words, everything is working exactly as it's supposed to. The problem is just with the URL you're using for your CSS file.

To make relative links resolve the way you want them to, you need to do one of the following:
1) Specify an absolute or root-relative URL in the link (e.g. http://yourhost/comm...ss/whatever.css or /common/css/whatever.css - note the leading slash on the latter).
2) Use the base tag to explicitly specify the base URL the browser should use to resolve relative links.

This post has been edited by AdaHacker: 07 February 2012 - 11:31 AM

Was This Post Helpful? 1
  • +
  • -

#4 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 118
  • View blog
  • Posts: 1,257
  • Joined: 01-June 09

Re: Apache mod_rewrite not rewriting

Posted 07 February 2012 - 07:30 PM

Well I've been working on this all day and finally am close to something I'm happy with. With the exception of one problem I'm having.

First of all, here's my newest .htaccess file:

RewriteEngine On
Options FollowSymLinks

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*/\ HTTP/
RewriteRule ^(([^/]+/)*([^/.]+))/$ $1 [R=301,L]

RewriteRule ^([^/.]+)$ index.php?cmd=$1 [L,QSA]

RewriteRule ^([^/]+)/([^/.]+)$ index.php?cmd=$1&action=$2 [L,QSA]

RewriteRule ^([^/]+)/([^/]+)/([^/.]+)$ index.php?cmd=$1&action=$2&ajax=$3 [L,QSA]




The bottom 3 rewrite rules are all self-explanatory and just do what I did in one in my post from above in 3 rules instead of 1. Easier to read for sure.

The condition and rewrite rule at the top are there because in my bottom 3 rules I only allow for urls that don't end in a trailing slash. This is to avoid Duplicate Content. Now though, if the user ends it in a slash I don't want it to fail. I want it to be redirected. This is what the top is suppose to be doing.

The condition checks that it ends in a slash, and if it does the rule should chop it off and redirect.

My problem is when I put in a url like this:
localhost/myProject/yes/

It isn't just chopping it off of that, it's chopping it off of the absolute path. On my screen I get this error:

Forbidden

You don't have permission to access /C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/myProject/y on this server.


Anyone have a solution to this problem? Been trying to fix it all day and can't get anything going.

Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1