Help With Mod Rewrite?

Hi guys, I have a dilemma. I am trying to make it so any file typed into the browser gets redirected to bootstrap.php. However, I want to exclude certain directories and any files in that directory from being redirected, they would just be displayed normally. Does anyone know how to do this or have a link? For instance I would like to redirect them like bootstrap.php?page=PAGE

Thanks,
Darren

Take a look at RewriteEngine, [url=http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteRule]RewriteRule, [url=http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond]RewriteCond and [url=http://www.datakoncepts.com/seo]this article by dklynn.

Here is what I tried:
RewriteEngine on
RewriteCond $1 !(css|images|js|media|system|themes|favicon\.ico|robots\.txt) [NC]
RewriteRule ^(.*)$ /_global/bootstrap.php?page=$1 [QSA,L]

Can you tell me what I am doing wrong? Basically the RewriteCond is supposed to say if it is in either a css, images, js, media, system, or themes directory to not Rewrite it, or if it is a favicon.ico or robots.txt file. This is supposed to be any of these directories or files located anywhere, but it gives me a 500 error.

Thanks,
Darren

You should also add bootstrap.php to the list of files not to be rewritten to avoid infinite rewrite loops.
Also, $1 in the RewriteCond won’t work, use %{REQUEST_URI} instead.

Also, you might want to take a look at %{REQUEST_FILENAME} in conjunction with !-d or !-f for RewriteCond.
You would need two RewriteConds, one with !-d and one with !-f

Thanks, I replaced it and it worked. As for as the -f and -d I do not want to do that because even if a file exists I want to forward it.

:tup:

Indeed, but, since we have NO idea what he wants to do with bootstrap, this may not be feasible (although it should be).

Regards,

DK

Hi dk, thanks for joining in. The reason is because I want to avoid some directories and files at the same time, making users not able to see them.

Hi Darren,

Okay, it IS easier to avoid those directories/files in mod-rewrite than to handle those requests via bootstrap.

On the assumption that you DON’T use CAPS in your file extensions, drop the No Case flag and use ScallioXTX’s %{REQUEST_URI} in your RewriteCond, add another RewriteCond to exclude _global/bootstrap.php and you should be set.

Your “refusal” to use -f and -d may cause you problems, but only IF bootstrap will attempt to redirect to existing files (i.e., LOOP), so you need to think this through to avoid problems. More help will require some knowledge of bootstrap’s function.

Regards,

DK

Hello DK, thanks for your insight again.

Here is my whole rewrite code:


RewriteEngine on
#RewriteCond %{HTTP_HOST} !^www\\.website\\.com$ [NC]
#RewriteRule .? http://www.website.com%{REQUEST_URI} [R=301]
#RewriteCond %{HTTP_REFERER} !^$
#RewriteCond %{HTTP_REFERER} !^http://(www\\.)?website\\.com/ [NC]
#RewriteRule \\.(swf)$ - [F]
RewriteCond %{REQUEST_URI} (_cache|_global|_templates|_templates_c)
RewriteCond %{REQUEST_URI} !(css|images|js|media|system|themes|favicon\\.ico|robots\\.txt)
RewriteRule (_cache|_global|_templates|_templates_c) /_global/bootstrap.php?page=404
RewriteRule ^(.*)$ /_global/bootstrap.php?page=$1 [QSA,L]

Some are commented out because I am working from localhost right now. I am getting an error 500. I have decided that I want to send any requests that try to access a url that contains _cache|_global|_templates|_templates_c to the bootstrap’s 404 page. Do you know what I did wrong?

Also the bootstrap takes a code file and a template file then fuses them together to output a page. I don’t know if that matters in the terms of this but that is what it does.

Thanks,
Darren

As said before you should prevent bootstrap.php from being rewritten, as you now have an infinite redirection loop in your code, which does indeed give you a HTTP 500 error.

You should do it like this:


RewriteEngine on
#RewriteCond %{HTTP_HOST} !^www\\.website\\.com$ [NC]
#RewriteRule .? http://www.website.com%{REQUEST_URI} [R=301]
#RewriteCond %{HTTP_REFERER} !^$
#RewriteCond %{HTTP_REFERER} !^http://(www\\.)?website\\.com/ [NC]
#RewriteRule \\.(swf)$ - [F]
[COLOR="RoyalBlue"]# You also need to prevent rewriting bootstrap.php here, as bootstrap.php is in the _global directory[/COLOR]
RewriteCond %{REQUEST_URI} (_cache|_global|_templates|_templates_c)
[COLOR="Red"]RewriteCond %{REQUEST_URI} !bootstrap\\.php$[/COLOR]
RewriteRule .? /_global/bootstrap.php?page=404

RewriteCond %{REQUEST_URI} !(css|images|js|media|system|themes|favicon\\.ico|robots\\.txt)
[COLOR="Red"]RewriteCond %{REQUEST_URI} !bootstrap\\.php$[/COLOR]
RewriteRule ^(.*)$ /_global/bootstrap.php?page=$1 [QSA,L]

The RewriteCond %{REQUEST_URI} !(css|images|js|media|system|themes|favicon\.ico|robots\.txt)
was in the wrong place, that should be before the last RewriteRule, as they have nothing to do with the RewriteRule before that.

Also, if you have


RewriteCond %{REQUEST_URI} (_cache|_global|_templates|_templates_c)

You don’t need to match (_cache|_global|_templates|_templates_c) in the following RewriteRule, since requests that do not match the RewriteCond will not be processed by the RewriteRule, so you’re checking something you already know to be true.

Y’all!

Don’t forget the LAST flag to separate your mod_rewrite block statements!

Regards,

DK

Thanks guys, I added the Last tag to the one above. I only have one issue. Is there any reason it shows the ?page=404 in the browser url when it does redirect?

Is there any reason it shows the ?page=404 in the browser url when it does redirect?

Well, if it went purely though the rewrite, it shouldn’t… but is bootstrap itself somehow doing any redirecting?

If something is redirected, the “new” url is seen in the browser addess bar. If something is only rewritten, then what the user typed that matched is seen in the browser address bar. Since the rewrite looks ok, if the browser is showing ?page=404 and that’s NOT what’s being typed in by the user, then somewhere there’s a redirect going on.

At least, that’s my guess.

Darren,

I’m sure that your mod_rewrite code has changed from the above so let me answer with Apache-type answers:

Redirected URLs will be displayed under ONLY two conditions:

  1. Use of an absolute redirection (although, I believe that internal redirections are exempt from this), i.e., http://www/yadda-yadda.com/path/to/file.

  2. Use of the R=301 directive in the flag associated with the RewriteRule which performed the redirection.

I see neither of those in your last posted mod_rewrite code so I may be wrong about the internal absolute redirection.

Regards,

DK

And in PHP: the use of header(“Location: /some/new/location”);

Hi guys I removed the exclusion of bootstrap.php because it is in the folder _global anyways.

And you’re not getting an internal server error? (you should)
If not, could you post your .htaccess, I’d be interested to see what you did exactly.

Btw, did you solve the problem of the URL in the URL bar changing?