Relative file path issue while using Apache rewrite rules
I am trying to create some Apache rewrite开发者_JAVA百科 rules that shall be able to route URI requests like e.g.
http://mydomain.com/articles/example
to
http://mydomain.com/index.php?site=articles&page=example
What I've got so far is this:
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z0-9_]+).html$ index.php?site=$1&page=$2 [L]
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z0-9_]+)$ index.php?site=$1&page=$2 [L]
Since I am using relative file paths everywhere in my index.php, for style sheets, scripts, images and the like, I am facing the problem of incorrectly resolved absolute file paths.
For example when I am in the root, http://mydomain.com/
or http://mydomain.com/articles
, all relative image paths resolve correctly - images/logo.jpg
becomes http://mydomain.com/images/logo.jpg
or htdocs/mydomain/images/logo.jpg
respectively.
When I click one of my links on the site, e.g. http://mydomain.com/articles/example
, Apache (or the Browser?) assumes all my images are located here http://mydomain.com/articles/images/
- it is pretty obvious that this path doesn't really exist.
So here are my questions:
1. Is it possible to solve this problem without changing all my relative file paths to absolute ones?
I could use root /
for all my paths on my actual web server, because my domain is advantageously linked.
mydomain.com
is linked to /html/mydomain
on my web server - /
resolves to mydomain.com/
, but on my personal computer I am running XAMPP and /
resolves to htdocs/
- it should however resolve to htdocs/mydomain/
simply because I hold several sites in htdocs
Absolute file paths are terrible when updating index.php to my web server, because I have to auto-systematically replace path declarations - not only in index.php, but style sheets and scripts as well.
2. Should I create a static DNS entry on my personal computer to adapt to my web server?
3. What is the most commonly used method?
Thank you in advance!
Sincerely, Sebastian
I figured it out by myself, by editing Windows' hosts
file and the Apache's httpd-vhosts.conf
file.
1. Adding a new static DNS entry to %SystemRoot%\system32\drivers\etc\hosts
127.0.0.1 www.mydomain.local
2. Appending a new virtual host entry to %ApacheInstallDir%\conf\extra\httpd-vhosts.conf
<Directory "E:\htdocs">
Order Deny,Allow
Allow from all
</Directory>
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
DocumentRoot "E:\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost 127.0.0.1>
DocumentRoot "E:\htdocs\mydomain"
ServerName www.mydomain.local
</VirtualHost>
Now http://www.mydomain.local
leads to htdocs/mydomain
directly
3. Changing all relative paths and links in my index.php by simply "rooting" them with a /
in front of them, e.g. <img src="/images/logo.jpg" />
I also managed to set up a Site in Dreamweaver, so I am able to use the Design- and the Live-View correctly.
精彩评论