Forum Moderators: phranque

Message Too Old, No Replies

Relative path in .htaccess files?

RewriteRule ^([\w\-_]+)/?$ ./index.php?page=$1 [NC]

         

jazzle

7:42 pm on Aug 19, 2010 (gmt 0)

10+ Year Member



edit2:
SOLVED by myself, my RewriteBase directive was confusing matters, BUT I would still be interested to know how to refer to the directory in which the .htaccess file is.

Hi guys and gals,

apologies in advance for what is probably a silly question.

I am developing several sites on localhost, though they will each be deployed at their own domains.
Each project is in a folder named as per the domain at it will eventually reside.

Assuming //localhost/ is configured with DocumentRoot as C:\www\
(I'm mentioning that it's on Windows just in case it matters, but I doubt it.)

C:\www\.htaccess
C:\www\index.php
C:\www\example.com\.htaccess
C:\www\example.com\index.php
C:\www\example2.com\.htaccess
C:\www\example2.com\index.php

where C:\www\index.php simply lists the subfolders as shortcuts for me.

C:\www\.htaccess contains simply
Options -Indexes


and C:\www\example.com\.htaccess contains
RewriteEngine On
RewriteBase /
RewriteRule ^([\w\-_]+)/?$ ./index.php?page=$1 [NC]


I assumed that I would be able to visit
//localhost/example.com/contact
and this would be rewritten to
//localhost/example.com/index.php?page=contact,
but it seems to be missing the subfolder and is instead going to
//localhost/index.php?page=contact

I'm surprised that ./index.php in the .htaccess doesn't either error or work as I expected.

Any help/suggestions/advice will be massively appreciated.



----
edit: I added
RewriteEngine On
RewriteBase /
above. I realise now that the RewriteBase directive may in fact be part of the problem.

g1smd

10:40 pm on Aug 19, 2010 (gmt 0)

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



I never use ./ notation as it is just too confusing when you start moving things around.

jdMorgan

3:57 am on Aug 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A better long-term solution for development is to use the name-based virtual hosting capabilities of Apache to avoid the necessity of doing any rewriting to support multiple hosts.

Simply define a new virtual host for each new domain, and then point that domain to the server's IP address in your computer's "hosts" file. This file exists on all internet-capable computer, and is always called just "hosts" -- all lowercase, and no file extension. It is generally located in the OS directories -- It's at C:\Windows\System32\drivers\etc\hosts on XP, Vista, or Win7, for example.

In this way you can properly and separately define a DocumentRoot for each domain, and you won't have the problem of redirects in .htaccess 'exposing' the rewritten-hostname-subdirectory filepaths to clients as URLs as you might have now...

Added: Note that underscores are already included by the "\w" token, so there is no need to also explicitly include "\_" in your alternate-character-group pattern.

Jim