Forum Moderators: phranque

Message Too Old, No Replies

How to write "NOT" for groups in mod_rewrite rules?

mod_rewrite and "NOT" for groups

         

cyberbob

9:44 pm on Oct 7, 2004 (gmt 0)

10+ Year Member



Hello All!

I use this rule to add end slash:
RewriteRule ^en/(.+[^/])$ /en/$2/ [R]

It works good for pages like:
/en/testpage -> /en/testpage/

Please tell me what should I write there if I haven't to add slash for pages which end with ".html" and ".php". I mean, if user ask for /en/testpage.html he have to get it.

Also please tell me how could I write NOT for a group? For skipping endings: ^(\.html)¦(\.php)? It doesn't work! :(

I need this asap.

Thanks to all in advance.

Bob.

jdMorgan

1:31 am on Oct 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bob,

Welcome to WebmasterWorld!

Try another method. Another way to do what your code above does is this:


RewriteCond %{REQUEST_URI} !/$
RewriteRule ^en/(.*) /en/$1/ [R=301,L]

That is, if the URL does not end in slash, and the requested directory is "/en", then add a slash.

Expanding that to avoid rewriting php and html files:


RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.(html¦php)$
RewriteRule ^en/(.*) /en/$1/ [R=301,L]

Posting on this board changes solid pipe characters to broken "¦" pipes. Be sure to edit the code snippets above and change them back to solid pipes before trying to use the code.

Unless you have a very good reason for it, do not use "[R=302]" or "[R]" (which is the same as [R=302]). In almost all cases where an external redirect is desired, it should be a permanent redirect. A 302 temporary redirect should only be used when the original URL is correct and will "return to service" in a short period of time. Using a 302 redirect when a 301 is needed can cause all kinds of trouble (see the recent threads in the Google forum about meta-refresh and 302 page-jacking).

Jim