Forum Moderators: phranque

Message Too Old, No Replies

Complicated Rewrite rules for .htaccess

         

BenFitts

9:06 pm on Feb 13, 2004 (gmt 0)

10+ Year Member



Hi everyone,

I've been reading through some of the .htaccess discussions and I am looking for help.

I'm part of a network marketing company and I have written a recruiting web site for all members of my team. I want them to be able to have their own custom version of the site. I want to do something like this:

[mydomain.com...]
[their_login.mydomain.com...]

And have them redirect to:
[mydomain.com...]

My problem is that I need to exclude a few files, like index.php, admin.php, etc. So I probably want to redirect everyone except those that are already pointed at a .php file.

Does that make sense?

Thanks in advance for your help!

jdMorgan

6:34 pm on Feb 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BenFitts,

Welcome to WebmasterWorld [webmasterworld.com]!

Files can be excluded easily from a RewriteRule directive by preceding it with a RewriteCond directive checking the %{REQUEST_URI} server environment variable.

We'll be happy to help you debug your code once you make a start.

Ref: Introduction to mod_rewrite [webmasterworld.com]

Jim

BenFitts

6:05 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



This is what I'm currently using and everything seems to be ok.

I basically took this script from the url rewriting guide at apache.org:


RewriteEngine on

# we are reached via / prefix
RewriteBase /

# first we rewrite the root dir to
# the handling cgi script
RewriteRule ^$ index.php [L]

# and now break the rewriting for local files
RewriteRule ^index\.php.* - [L]
RewriteRule ^admin\.php.* - [L]
RewriteRule ^removeme\.php.* - [L]
RewriteRule ^images.* - [L]
RewriteRule ^photos.* - [L]

# anything else is a subdir which gets handled
# by another cgi script
RewriteRule!^index\.php.* - [C]
RewriteRule (.*) index.php/$1

If you see any flaws, please let me know?

<snip>
No question mark, no passing of arguments needed, so if someone forgets they'll be redirected properly

This also has the added benefit of routing any 404 type errors to my script

Thanks,
Ben Fitts

[edited by: jdMorgan at 10:41 pm (utc) on Feb. 16, 2004]
[edit reason] No URLs, please. See TOS. [/edit]

jdMorgan

10:59 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could crunch that down a bit:

RewriteRule ^$ index.php [L]
RewriteRule ^((index¦admin¦removeme)\.php¦images¦photos) - [L]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule (.*) index.php/$1 [L]

The only disadvantage of this approach is that all subsequent rules will be bypassed if any one these rules matches and is invoked. Make sure that this code follows all access-control code such as IP and user-agent blocking, otherwise that code will never execute.

> This also has the added benefit of routing any 404 type errors to my script

This is an extremely dubious "benefit". If search engines detect that your site will never return a 404, they will arbitrarily limit the depth to which they spider your site. I suggest you do *not* do this, unless your script is capable of generating a correct 404 server response for resources which are not available.

Redirecting all 404s to a catch-all script-generated page invokes the spectre of duplicate-content filters and limited SE spidering to avoid an apparently-infinite URL-space, neither of which is good for your site's ranking. Server response codes [w3.org] have specific meanings, and cause specific user-agent actions. Be sure you know what they mean and return the appropriate code.

Replace all broken pipe "¦" characters above with solid pipe characters from your keyboard before use.

Jim