Redirect all initial requests

New to mod_rewrite? This is a good place to start.

Postby richardk » Tue Aug 18, 2009 4:15 pm

I have realised that there are some subfolders in the directory that I am dealing with that I do not want to rewrite to the tracking script at all so I need to put in a condition that does not rewrite requests to:

/directory/sub1
/directory/sub2
/directory/sub3

Not quite sure on the correct syntax for doing that.

Try
Code: Select all
RewriteCond $1 !^(sub1|sub2|sub3)(/.*)?$ [NC]

or
Code: Select all
RewriteCond %{REQUEST_URI} !^/directory/(sub1|sub2|sub3)(/.*)?$ [NC]


Also I use domain aliases so is it possible to use something like the following in place of example.com:

You cannot use variables in RewriteCond (or RewriteRule) patterns. Try
Code: Select all
RewriteCond %{HTTP_HOST}/%{HTTP_REFERER} !^([^/]+)/\1(/.*)?$ [NC]


Finally is the rewrite rule:
RewriteRule ^(.*)$ /Tracking/index.php?Path=directory/$1 [NC,QSA,L]

still root relative or is it realtive to the directory where the .htaccess file is residing?

Relative to the .htaccess file.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alhermette » Wed Aug 19, 2009 5:54 am

Relative to the .htaccess file.


So what would the correct syntax be for making the rewrite rule relative to the root (I am only one directory deep)?

/Tracking/index.php?Path=directory/$1
~Tracking/index.php?Path=directory/$1
../Tracking/index.php?Path=directory/$1

Or am I forced to use an absolute path (which I prefer not to do because of the domain alias use).
alhermette
 
Posts: 18
Joined: Mon Mar 16, 2009 2:44 pm

Postby richardk » Fri Aug 21, 2009 5:58 am

I'm confused about what you want.

Code: Select all
Syntax:   RewriteRule Pattern Substitution [flags]

The regular expressions (pattern) matches the part of the URL relative to the current directory. Ie. for ^(.*)$ it will be path/ not directory/path/.

A relative path in the Substitution is relative (./path/ or path/) to the current directory. A (semi) absolute path in the Substitution is absolute (/path/ or http://www.example.com/path/) to the current directory.

What problem are you having?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alhermette » Sat Aug 22, 2009 12:18 pm

Sorry I think I confused things with the way I posted.

The .htaccess file that we are talking about is in a directorty. The decision that is being made here is do we rewrite to the tracking file or not. If the answer is yes then the tracking file resides at www.mydomain.com/Tracking.index.php whereas the directory where the .htaccess resides is www.mydomain.com/Other-Directory.

What I am after is the syntax for rewriting relative to the root as opposed to relative to the directory where the .htaccess file resides. I could obviously use an absolute path but as I use domain aliases it would be preferable to use a root relative link if that is possible.

When the .htaccess file is in the root the rewrite rule is:

RewriteRule ^(.*)$ /Tracking/index.php?Path=$1 [NC,QSA,L]


Now that I have transferred the rewrite rule to a directory, but still need to rewrite to the same file, I need to tell it to jump up a level otherwise I will end up rewriting to

www.mydomain.com/Other-Directory/Tracking/index.php


Which doesn't exist. Hope that clarifies matters
alhermette
 
Posts: 18
Joined: Mon Mar 16, 2009 2:44 pm

Postby richardk » Mon Aug 24, 2009 2:11 pm

The same rule should work fine. /Tracking means /Tracking (not /currentdirectory/Tracking). Have you tried it?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Previous

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 33 guests

cron