简体   繁体   中英

Rewrite to relative path in Apache2 on Mac OS X Lion (10.7)

I've searched the site thoroughly and Googled for this as well, but to no avail. I use Apache2 + PHP on my Mac OS X. I haven't changed much of the configuration on any of them, just enough to get everything working correctly. Here's my .htaccess file:

<IfModule mod_rewrite.c>

Options +FollowSymLinks
Options +Indexes

RewriteBase /~milad/mysite/

RewriteEngine On

RewriteRule ^$ index.php?:url [L,QSA] #Handling tail with no parameters

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)$ index.php?:url=$1 [L,QSA]

</IfModule>

This works just fine for all the files located at http://localhost/~milad/mysite/* . But I want to make my .htaccess file independent of my installation's particulars, ie, I don't want to have to include the RewriteBase ... line in my code.

But when I remove that line, Apache tries to map it to a URL which I don't understand. What I get is:

Not Found

The requested URL /Users/milad/Sites/newave/index.php was not found on this server.

which is just ridiculous, because the index.php file is JUST where that URI is pointing to. Anyway, when I try to rewrite to /index.php instead of index.php , I find that it is being rewritten to http://localhost/index.php . So how is it that I can't use just the relative path, or just use ./index.php (Yes, I've tried that, too).

Any insight would be greatly appreciated.

The problem comes from your vhost configuration:

Not Found
The requested URL /Users/milad/Sites/newave/index.php was not found on this server.

Means clearly: "the base of your site is /Users/milad/Sites/newave/ ". So if you want the base for you site to be /~milad/mysite/ try to change the DocumentRoot

<VirtualHost *>
    ServerName mysite.com
    DocumentRoot "/~milad/mysite/"

</VirtualHost>

Of course, all the files of you site must be in the folder " /~milad/mysite/ ".

Then in your htaccess file (notice that ^(.*?)$ is equivalent to (.*) and I don't understand why you need the RewriteRule ^$ because it should be handled properly at the end):

Options +FollowSymLinks
Options +Indexes

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?:url=$1 [L,QSA]

Please tell me if it works


Edit: Following the comments, here's a vhost file that works for me:

<VirtualHost *>
    DocumentRoot "/web/htdocs/olivier/wwog"
    ServerName wwog.fr
    ServerAlias *.wwog.fr

    ErrorLog "/web/logs/wwog.error.log"
    CustomLog "|/opt/httpd/bin/rotatelogs /web/logs/wwog/access.%Y-%m-%d-%H_%M_%S.log 5M" combined

    DirectoryIndex index.php index.htm

    <Location />
        # Compression:
        # (http://httpd.apache.org/docs/2.0/mod/mod_deflate.html)
        # Insert filter
        AddOutputFilterByType DEFLATE text/html text/plain
        SetOutputFilter DEFLATE

        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip

        # Don't compress images
        SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png)$ no-gzip dont-vary

        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </Location>

    # Add "Cache-control: public" = valid for 480 weeks
    # for proxies to keep images in cache:
    <FilesMatch "\.(ico|flv|jpg|jpeg|png|gif|swf)$">
        Header set Cache-Control "max-age=290304000, public"
    </FilesMatch>

    # Forbid files that start with "_"
    <FilesMatch "^_">
        Order allow,deny
        Deny from all
        Satisfy all 
    </FilesMatch>

    # Forbid .htaccess and .htpasswd
    <FilesMatch "^\.ht">
        Order allow,deny
        Deny from all
        Satisfy all 
    </FilesMatch>

    <Directory "/web/htdocs/wwog">
        Order allow,deny
        Allow from all
        Deny from none
        AllowOverride All 
    </Directory>

</VirtualHost>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM