|rewriteRule directive problem|
| 1:42 pm on Feb 11, 2007 (gmt 0)|
I have been trying for some time to compose a generic rewriteRule in .htaccess for a site hosted on a university server - one for which I have absolutely no control what-so-ever, except for a specific folder accessible with the URL say [uni.ac.uk...]
this is what I am trying to achieve:
[uni.ac.uk...] -> [uni.ac.uk...]
The rewrite is relatively simple and works on two servers I have tried this on except the above:
RewriteRule \.(xml¦txt¦png¦gif¦jpg¦css)$¦^index.php$ - [S=1]
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]
However, what seems to work is:
rewriteRule ^([^/]+)/$ http://uni.ac.uk/lab/new/index.php?page=$1 [L]
Note that I have not been able to:
- exempt for xml,txt,css, etc etc files,
- use the '?' or '*' quantifier to designate the absence of a trailing slash (!)
- designate index.php as the substitution string unless preceded by the complete URL.
I do realise that the rewrite is per-directory based and that the server will have its own rules too. I now know what works and what not works but have not been able to progress from here for some days. Any help is much appreciated in this matter.
| 6:07 pm on Feb 11, 2007 (gmt 0)|
|exempt for xml,txt,css, etc etc files, |
Put this before your rewrite rule - it will exclude existing files and directories.
For the rest of your problems, you'll just need to practise you regular expressions :-)
| 6:27 pm on Feb 11, 2007 (gmt 0)|
Be aware that checking each and every HTTP request for "file exists" and "directory exists" as suggested above is expensive in terms of server resources. I recommend that you avoid this technique if at all possible, or make those checks conditional on a RewriteRule pattern match or on preceding RewriteCond pattern matches.
On the original subject, where is this code located? Is it located in example.com/.htaccess or in example.com/lab/.htaccess?
| 7:02 pm on Feb 11, 2007 (gmt 0)|
thanks for looking into this.
I should be able to exempt redirects on extension as far as I know, which in my opinion is the most efficient way. Re. regular expressions, they're both practised and practiced.
Since I only have limited access to the server, I can only drop files into www.example.com/lab/ where index.php is found as well.
| 7:04 pm on Feb 11, 2007 (gmt 0)|
...and that is where my .htaccess code is located as well. ;)
| 7:21 pm on Feb 11, 2007 (gmt 0)|
Let's try a slightly-different approach, then:
RewriteCond $1 !\.(xml¦txt¦png¦gif¦jpg¦css)$
RewriteCond $1 !index\.php$
RewriteRule ^(([^/]+/)*[^/]+)/?$ /lab/index.php?page=$1 [L]
The option to disable multiviews may help with your trailing-slash problem, and the modified RewriteRule pattern may help if any files are located in subdirectories of /lab/
Replace all broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.
If this does not help, please tell us what specific error messages you get, or any information about the server response that may be of diagnostic use; It's difficult to diagnose problems without a description of the symptoms. Also if possible, please report the contents of the server error log file when testing the code.
| 10:13 pm on Feb 11, 2007 (gmt 0)|
That's a hole in one! And only seems to work with multiviews disabled. Very strange they were enabled in the first place. Not something I would have considered looking into straight away..
Fantastic. Thanks a bunch.