I apologize in advance if this is too long. I figure more detail is better than less and hope I'm not being horribly rambling :-)
I use WAMP on my laptop for local dev, and I have various c:/wamp/alias/*
files each pointing to a project working directory. I've had some excellent mod_rewrite help and gotten
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*\/)?ih(\/.*)?$ $1index.php$2 [L,QSA]
to work to change localhost/.../ih/sub/dir
to localhost/.../index.php/sub/dir
for URLs that are both SEO-friendly and short. [I haven't gone on to do this in prod but I suspect it will work just as well.] However, to get it all together I had to change my doc root from c:/wamp/www/
to c:/
, which I'd really rather not do just in case my Apache gets hacked and otherwise because it's a kludge.
My test alias file looks like
Alias /testme "c:/var/tmp/wamp-testme/"
<Directory "c:/var/tmp/wamp-testme/">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*\/)?ih(\/.*)?$ $1index.php$2 [L,QSA]
</Directory>
and the error I get when trying to load http://localhost/testme/rewrites/ih/sub/path
is
[Thu Jun 30 06:46:13 2011] [error] [client 127.0.0.1]
File does not exist: C:/wamp/www/var
with a matching
Not Found
The requested URL /var/tmp/wamp-testme/rewrites/index.php/sub/path
was not found on this server.
in the browser. Sure enough, the same config in my c:/wamp/alias/flying.conf
file that points to the c:/data/flying/
directory throws File does not exist: C:/wamp/www/data
in the error log file, and so on.
Sooooo... How can I have a rewrite rule that transcends aliases without having my doc root at my machine root dir?
A rewrite rule can be invoked in httpd. conf or in . htaccess . The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.
In your rewrite, the ^ signifies the start of the string, the (. *) says to match anything, and the $ signifies the end of the string. So, basically, it's saying grab everything from the start to the end of the string and assign that value to $1.
RewriteRule specifies the directive. pattern is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser. substitution is the path to the actual URL, i.e. the path of the file Apache servers. flags are optional parameters that can modify how the rule works.
htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.
Odd: try adding a PT
flag to the rewrite rule: this forces the re-written URL to be sent back to the URL mapping engine.
EDIT: try setting the RewriteBase
on the Directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With