...

View Full Version : MOD Rewrite Problem with PHP Pages



macleodjb
05-02-2009, 01:54 PM
Hi guys,

I have a site which is fully dynamic and generated from a Mysql database. I am having an issue with my .htaccess mod rewrite. The problem i am having is that my urls work fine, however my css file seems to be failing to load. I'm not so good with mod rewrite so i was hoping someone out there has a simple solution for me.

This is the structure of my URL

http://www.example.com/category/subcat

and this is what is inside my .htaccess file at my root directory.



RewriteEngine On
RewriteRule ^go/(.+)/(.+)$ hop_redirect.php?id=$2&%{QUERY_STRING}
RewriteRule ^(.+)/(.+)$ search.php?q=$2&%{QUERY_STRING}
RewriteRule ^(.+)/(.+)/(.+)$ view_product.php?id=$3
RewriteCond $1 !^(index\.php|images|files|fckeditor|robots\.txt|css)[L]


and this is how i call my css within the head of my pages



<link rel="stylesheet" media="all" type="text/css" href="css/menu.css" />


I tried switching it to the full http directive but it still doesn't seem to load.

Any help is appreciated. Thanks alot.

abduraooft
05-02-2009, 02:01 PM
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^go/(.+)/(.+)$ hop_redirect.php?id=$2&%{QUERY_STRING}
RewriteRule ^(.+)/(.+)$ search.php?q=$2&%{QUERY_STRING}
RewriteRule ^(.+)/(.+)/(.+)$ view_product.php?id=$3
RewriteCond $1 !^(index\.php|images|files|fckeditor|robots\.txt|css)[L]

macleodjb
05-02-2009, 02:18 PM
I tried that but was unsuccessful. Any other thoughts?

A strange note:


If i put a forward slash in front of the $ sign my css works but my url rewrite does not.

venegal
05-02-2009, 02:23 PM
I'm not sure that will work, since the RewriteConds only modify the next rule, and there are still two more that can hijack that css.

Putting it like this should work though:


RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ - [L]
RewriteRule ^go/(.+)/(.+)$ hop_redirect.php?id=$2&%{QUERY_STRING}
RewriteRule ^(.+)/(.+)$ search.php?q=$2&%{QUERY_STRING}
RewriteRule ^(.+)/(.+)/(.+)$ view_product.php?id=$3
RewriteCond $1 !^(index\.php|images|files|fckeditor|robots\.txt|css)[L]

macleodjb
05-02-2009, 02:41 PM
That worked perfectly. Thanks alot.

misterm
05-02-2009, 03:18 PM
Nice, I have been looking for a solution like this as well.

macleodjb
05-02-2009, 05:25 PM
I came across another issue. I am trying to implement a feed on the same website and i inserted another rewrite rule and it doesn't seem to be working properly.

Here is what i inserted


RewriteRule ^feeds/(.+)/(.+)$ feeds/feeds.php?cat=$2%{QUERY_STRING}


This is how my .htaccess file looks now.


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [or]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ - [L]
RewriteRule ^go/(.+)/(.+)$ hop_redirect.php?id=$2&%{QUERY_STRING}
RewriteRule ^feeds/(.+)/(.+)$ feeds/feeds.php?cat=$2%{QUERY_STRING}
RewriteRule ^(.+)/(.+)$ search.php?q=$2&%{QUERY_STRING}
RewriteRule ^(.+)/(.+)/(.+)$ view_product.php?id=$3
RewriteCond $1 !^(index\.php|images|files|fckeditor|robots\.txt|css)[L]


I do have the folder named "feeds" on the testing server as well as a file within that folder named "feeds.php", however its giving me a 404 error.

I wish i could get my head around how mod rewrite works so i could troubleshoot my own problems. But i'm not there yet so if you are so kind to help me out again i'd appreciate it. Thanks again.

venegal
05-02-2009, 06:25 PM
I'm not sure if this will solve your problem, but one thing that's going wrong with your script is the fact that you did not tell it to consider the added rule as the last one if matched.

The way it is now, 'feeds/sth1/sth2' is rewritten to 'feeds/feeds.php?cat=sth2'.
Then the next rule kicks in and rewrites 'feeds/feeds.php?cat=sth2' to 'search.php?q=feeds.php?cat=sth2', which is definitely not what you want.

So, add [L] at the end of your new rule, or better yet, at the end of every rule you want to be the last one evaluated if matched.

Furthermore you forgot a & in your new rule.


RewriteRule ^feeds/(.+)/(.+)$ feeds/feeds.php?cat=$2&%{QUERY_STRING} [L]

macleodjb
05-02-2009, 08:25 PM
Thanks alot i'll give that a try. The heat was getting too intense for me so i just removed the rule from the original .htaccess file and added a separate file under the feeds directory and got it working. I'd rather keep it all in one file, so thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum