Forum Moderators: phranque

Message Too Old, No Replies

pattern not matched in htaccess

using htaccess to redirect when a pattern is NOT matched

         

citpes

8:18 pm on Nov 3, 2007 (gmt 0)

10+ Year Member



Hi all

I've been coding in php, javascript etc (the usual suspects) for quite some time, but have never taken the time to really learn regular expressions. Bad idea it seems :)
I'm new to websites and all the SEO, pretty urls etc that comes with it (I was a systems coder initially), and I've got myself a bit stuck.

I basically have a catch all rewrite rule to send everything to index.php, and I handle everything there. Its worked out just fine for me, Im happy having everything in one place.


RewriteEngine On
RewriteRule .*index.php[L]

The problem comes in when I try include a js or css file

<script src="js/myfile.js"></script>

The htaccess now seems to catch the url, and not include the file correctly.
The way Im thinking of fixing it is by trying to tell the htaccess to not redirect if the url has "js/" or "css/" in it. I just havnt a clue how to do that. The net is full of tutorials telling you how to get matches from almost anything, but I dont know how to NOT get the match :)

Hope that makes sense, Im notorious for over complicating things and confusing everyone around me.

Thanks in advance!

jdMorgan

10:56 pm on Nov 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The NOT operator "!" is implemented in mod_rewrite itself, rather than in regex.

RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !\.js$
RewriteCond %{REQUEST_URI} !\.css$
RewriteRule .* /index.php [L]

Alternate form using "local OR" and a back-reference to the rule pattern-match value instead of %{REQUEST_URI}:

RewriteEngine on
#
RewriteCond $1 !^index\.php$
RewriteCond $1 !\.(css¦js)$
RewriteRule (.*) /index.php [L]

Replace the broken pipe "¦" character in the RewriteCond with a solid pipe before use; Posting on this forum modifies the pipe characters.

Beware of rewriting URLs such as robots.txt and avoid using your script to handle the error pages for critical server errors. In both cases, the fewer dependencies associated with these resources, the better.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

[edited by: jdMorgan at 10:56 pm (utc) on Nov. 3, 2007]

citpes

6:53 am on Nov 4, 2007 (gmt 0)

10+ Year Member



Awesome! Thanks for the help. I'm definitely going to be reading up a whole bunch more on the subject of regex, as well as things like htaccess and how it affects a site.
So much to learn, so little time....

Thanks again

g1smd

1:55 am on Nov 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



By using a rewite to "send everything to index.php" you have created infinite Duplicate Content on the site. Every URL brings up the same content.

I would have used a 301 redirect to "/" so that only that URL could be indexed.