Forum Moderators: phranque

Message Too Old, No Replies

.htaccess code help

         

Marfola

1:03 pm on Feb 23, 2009 (gmt 0)

10+ Year Member



I'm trying to execute a do-redirect script before index.php to obviate TYPO3 for non-existent urls but can't seem to get the .htaccess right.

here's my htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*) fileadmin/scripts/do-redirect.php [C]
RewriteRule .* index.php [L]

The do-redirect isn't invoked at all. All non-existent URLs go directly to index.php.

How can I execute do-redirect.php before index.php?

jdMorgan

6:00 pm on Feb 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's because the rules are chained, so the initially-rewritten fileadmin/scripts/do-redirect.php path is immediately changed to /index.php.

What were you trying to do with the chaining function?

JIm

Caterham

7:38 pm on Feb 23, 2009 (gmt 0)

10+ Year Member



trying to execute a do-redirect script before index.php

You can't run two content handlers the same time for one request. How would you go back to the fixup phase from within the handler? Impossible.

What about including index.php into do-redirect.php via include() (or vis-a-vis)?

Marfola

12:34 pm on Feb 24, 2009 (gmt 0)

10+ Year Member



My goal is to manage all redirects www and non-www via the do-redirect script. I was hooping to manage the static files in .htaccess without calling script.

Would the following be considered a chain?


RewriteBase /

RewriteRule ^typo3 - [L]
RewriteRule ^typo3$ typo3/index.php [L]

#non-www to www redirect for .htaccess
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) fileadmin/scripts/do-redirect.php [L]

#www rule: exclude static files
RewriteCond %{HTTP_HOST} www\.example\.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* fileadmin/scripts/do-redirect.php [L]

Would it be more clear and compact to manage both rules in one?

 
RewriteBase /

RewriteRule ^typo3 - [L]
RewriteRule ^typo3$ typo3/index.php [L]
 
#call do-redirect
RewriteRule .* fileadmin/scripts/do-redirect.php [L]

and the do-redirect script, first of all checks if static files and then check the dynamic files

if (is_file("../.." . $uri)){
if ($non_www) {
page301('http://www.example.com' . $uri);
exit;
} else
exit;
} else
...

What is (or vis-a-vis)?

jdMorgan

4:33 pm on Feb 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This won't likely do what you intended (see comment lines):

# If requested URL-path starts with "typo3", skip all following rules
RewriteRule ^typo3 - [L]
# If requested URL-path starts is exactly "typo3", rewrite to /typo3/index.php
RewriteRule ^typo3$ typo3/index.php [L]

The problem is that the exact URL-path "typo3" will match the first rule, so the second will never be executed.

Please indicate what you are trying to do with those rules.

I believe that "or vis-a-vis" was intended to mean "or something like that."

Caterham's point is that once you call a script, control never returns to the server config files (e.g. .htaccess). So, whatever script you invoke with your rules, it must generate the entire server response. So, either it must generate a redirect, or it must generate the requested page content. As suggested, the easiest way is probably to call do-redirect, and if no redirect is required, then do-redirect should include (read-in and execute) index.php.

Or simply merge the two scripts, and include the do-redirect logic in index.php itself.

Jim

Jim

g1smd

7:55 pm on Feb 24, 2009 (gmt 0)

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



#non-www to www [b]redirect[/b] for .htaccess
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$

RewriteRule (.*) fileadmin/scripts/do-redirect.php [L]

This cannot possibly work. A redirect should contain both a host name in the target URL, and [R=301,L].

The code above is for an internal rewrite, and has nothing to do with www and non-www.

I am guessing that a line of code is missing, before that very last line.

Marfola

4:20 pm on Feb 26, 2009 (gmt 0)

10+ Year Member



First off, thanks for your patience.

My URLs are all dynamic. I use the realurl extension (typo3) to transform my dynamic URLs into static URLs (internal mapping).

My goal is to manage the both the internal mapping and any redirects (non-www to www, non-index to index) in the same place to avoid the possibility of a chain. I have two options: manage both in typo3 or manage both via a do_script called by .htaccess. (It is impossible manage the internal mapping in .htaccess.)

Managing both in typo3 means the internal URLs have to pass TYPO3 scripts before redirecting. This makes it slower and less efficient.

Managing both in a do_script means any redirects are managed immediately, before calling TYPO3.

Here’s my final .htaccess.
 
Options +FollowSymLinks
RewriteEngine On

RewriteBase /

RewriteRule ^typo3 - [L]

#script non-www to www redirect
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) fileadmin/scripts/do-redirect.php [L]

#script www redirect
#exclude static files like images, css, robots, javascript, …
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
#call index.php (TYPO3) where the do-redirect is included
RewriteRule .* index.php [L]

With regards to:

RewriteRule ^typo3 - [L]
RewriteRule ^typo3$ typo3/index.php [L]
The problem is that the exact URL-path "typo3" will match the first rule, so the second will never be executed.


You are quite right the second line is useless.

Thanks for all of your help.

[edited by: jdMorgan at 4:28 pm (utc) on Feb. 26, 2009]
[edit reason] example.com [/edit]