Forum Moderators: phranque

Message Too Old, No Replies

htaccess mod_rewrite driving me crazy

Pls help

         

niki77

1:19 am on Jul 7, 2006 (gmt 0)

10+ Year Member



Hi all.

Im looking for some help to setup a htaccess like below:

Code:

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://www.aaa.com/
RewriteRule /* http://www.example.com/aaa [R,L]
RewriteCond %{HTTP_REFERER} ^http://www.bbb.com/
RewriteRule /* http://www.example.com/bbb [R,L]
RewriteRule ^(.*)\.html$ index.php?id=$1 [L]

I have the code above but no luck to do what I want. I need if the referer is aaa.com then it redirects to example.com/aaa, then if the referer is bbb.com it redirects to example.com/bbb, and finally if none of above are coming from aaa.com nor bbb.com it writes the friendly URL ( last line. )

It is working to redirect people based on the referer, but I cannot make it to work to re-write the URL for those not refered by aaa or bbb. It simple shows a 404 error obviously because the URL has not been re-written

Noticed that there is no error in the code (in fact it works when added to the domain root folder), but is not working at all when uploading the in a higher directory level. Surely, it is not able to re-write at this top level.

Example:
Placing it at
http://example.com/.htaccess
or
home/httpd/vhosts/example.com/httpdocs/.htaccess
it works fine

But placing it 1 or 2 levels above like:
home/httpd/vhosts/.htaccess
it doesnt work, even when apache reads all levels to find 1 htaccess before serving the page.
In fact, the aaa.com and bbb.com redirections work fine, just having problems with the rewrite when placing it at this level.

Unfortunately, I must place it there, so wondering if there is something I can add to the last line so it can rewrite the rule. May be like adding the example.com somewhere there, but Im frozen trying to get it (yea, googled almost every tutorial with no luck)

Hope someone here can help me out

[edited by: jdMorgan at 2:28 am (utc) on July 7, 2006]
[edit reason] example.com [/edit]

jdMorgan

2:25 am on Jul 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot use .htaccess "above" the document root for a domain. By definition, it works in HTTP-accessible directories only.

Look into converting your code for use in httpd.conf or conf.d.

Jim

P.S. You can make your rule more efficient by changing the pattern:


RewriteRule ^([^.]+)\.html$ index.php?id=$1 [L]

niki77

2:57 am on Jul 7, 2006 (gmt 0)

10+ Year Member



Thanks for the tip

However I noticed the htaccess placed above the domain root actually works, in fact placing it there it works when redirecting based on the referer, the part that is not working is the rewriting one, but the redir does.

May be htaccess works but which not is the rewrite thing

Anyway, is just an idea coz Im a newbie with this all stuff

Thanks

Pfui

5:17 am on Jul 7, 2006 (gmt 0)

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



You cannot use .htaccess "above" the document root for a domain. By definition, it works in HTTP-accessible directories only.

Jim, I've been wondering about something related for a while. How about if a single .htaccess were placed in a directory housing numerous accounts, and then ScriptAliased (akin to some server-wide CGIs) to make it 'appear' in and be accessible to certain sub-directories?

/accountsdir
.htaccess
__ /account1
____ public_html
_______ .htaccess -> /dir1/dir2/accountsdir/.htaccess

Unauthorized eyes would still be blocked courtesy of httpd.conf:

<Files ~ "^\.ht">
Order deny,allow
Deny from all
</Files>

I ask because I could access accountsdir -- the server is co-located -- and uploading a single, revised, intentionally semi-global .htaccess would be easier than tweaking httpd.conf, and with no restarts.

Thoughts? TIA

jdMorgan

1:02 pm on Jul 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This goes to the definition. If the .htaccess flle (or perhaps a symlink to it) is HTTP-accessible, then it should work. Without digging into niki77's server config file, I can't tell why .htaccess is working above DocumentRoot, but this is apparently not an orthodox configuration.

I haven't tried the symlink approach, but it's worth looking into. Please let us known if you test it... :)

Jim

niki77

1:46 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



Hi Morgan

I found the htaccess privilegies "How directives are applied" at [httpd.apache.org...]

Further note that Apache must look for .htaccess files in all higher-level directories, in order to have a full complement of directives that it must apply. (See section on how directives are applied.) Thus, if a file is requested out of a directory /www/htdocs/example, Apache must look for the following files:

/.htaccess
/www/.htaccess
/www/htdocs/.htaccess
/www/htdocs/example/.htaccess

Hope it helps

jdMorgan

2:26 pm on Jul 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, but again, the definition of exactly what is meant by "/.htaccess" is what's critical here. The filepath is formed by removing the domain name from the requested URL, and appending whatever is left to the contents of the declared DocumentRoot path for the server. If your DocumentRoot is pointed to /html/public, and you request /foo.html, then the server path will be /html/public/foo, and anything above that should be inaccessible.

I would guess that on your server, DocumentRoot is pointed "too high" in the filesystem, or that --as mentioned above by Pfui-- you may have a symlink or Alias directive at work here.

Jim

niki77

3:31 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



Oh, I see
May be I have a rare server config.
However, do u think there is a way to rewrite it from a higher level since looks my server is allowing it by pointing docroot to a higher level?

I tried some combinations like:
RewriteRule ^domain.com/httpdocs(.*)\.html$ index.php?id=$1 [L]

RewriteRule ^(.*)\.html$ [domain.com...] [L]

Thanks anyway
pd: yes, Im a newbie with all this stuff, jeje

Pfui

6:43 pm on Jul 7, 2006 (gmt 0)

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



niki77, on most servers, users either need to be "root" or have special permission to be able to upload, alter, or even access files in directories above their user/account directory.

Having "root access" is like having the Master Key to all of a server's locks. It's a big deal -- hackers try to "get root" specifically to take over machines.

If you're on a shared server, typically only your ISP's SysAdmin-level employees have root capability/responsibility and they should be highly disinclined to give any user root, sorry. (Being a passenger on a plane doesn't mean you also get to pilot it:)

niki77

9:37 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



well, im the pilot in this case since mine is a dedicated one :)