Forum Moderators: phranque

Message Too Old, No Replies

Manipulate Server Variables

Can it be done?

         

Twister47

3:37 am on Oct 1, 2007 (gmt 0)

10+ Year Member



In htaccess, can you do anything with a server variable (such as REQUEST_URI) other then use the whole variable?

I'd like to do something like this:

User goes to /pageA.php
Htaccess looks to see if /old/pageA.html exists
if it does, then go to /old/pageA.html else
go to /pageA.php

I'd like to know if there is a way to strip the extension off of the REQUEST_URI. So, in other words, if you go to /pageA.php, be able to use just 'pageA'.

Hopefully that makes sense.

Thanks!

jdMorgan

9:20 pm on Oct 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For your specific example:

# If URL-path not already rewritten to /old/<requested_path>
RewriteCond %{REQUEST_URI} !^/old/
# and if <document_root>/old/<requested_path> exists as a file
RewriteCond %{DOCUMENT_ROOT}/old%{REQUEST_URI} -f
# then rewrite to /old/<requested_path>
RewriteRule ^(pageA\.php)$ /old/$1 [L]

When doing something like this, it is best to make the RewriteRule pattern as specific as possible, so that the CPU-expensive 'file exists' check can be avoided unless necessary. You might, for example, be able to make the pattern match only URL-paths beginning with "page," or match only those ending with ".php", or those that have numbers in them, etc. The more specific the pattern, the less CPU time you'll waste, and the faster your server will run.

For more information, see the documents cited in our forum charter [webmasterworld.com].

Jim