Applying a RewriteRule to a an Alias folder (overriding Aliases with RewriteRules)

I have an account (say “user1”) where httpd.conf contains an Alias to another account’s folder:

Alias /redir /home/user2/public_html/

I’d like to apply RewriteRules in .htaccess for the /redir folder, but .htaccess sems to just ignore the rules I try. For instance rewriting redir/test to the test.php for user1:

RewriteRule redir/test.php test.php 

This rule seems to simply be ignored - the URL redir/test goes to user2/public_html/test.php, if it exists, following the Alias. I also tried it with the full path

RewriteRule redir/test.php /home/user1/public_html/test.php 

What is the correct way to write RewriteRules that can override the Alias directive?

Kad,

You can’t. mod_alias (redirect) takes precedence over mod_rewrite so mod_rewrite can NEVER override a mod_alias directive.

Regards,

DK

Thanks. Then is there any way using .htaccess only to rewrite URLs for one account to file locations under another account? For instance placing something like this in the .htaccess of user1, which failed when I tried it:

RewriteRule redir/test.php /home/user2/public_html/test.php

Or by some other means.

And/or, perhaps there is some way to use regex in httpd.conf to create an Alias for everything except a certain file or folder(s)?

Is regex allowed inside an Alias directive?

Kad,

“account?” If you mean owned by different people, I certainly hope not (except with an absolute redirect)!

I don’t use Alias - even for cgi-bin - as it puts the files in other places (often out of the webspace).

WHAT are you REALLY trying to do?

Regards,

DK

I have a dedicated server, with two different cpanel accounts. I need to have a subfolder of one account deliver content from a script located on another account, and I need to apply some on top of that RewriteRules also.

For instance I want /user1/public_html/redir to deliver content from /user2/public_html/somescript.php.

A simple php include doesn’t quite work, because of path issues - the script is a CMS that uses a bunch of folders (e.g. /user2/public_html/images/…), and the browser doesn’t translate the path from domain.com/redir/images to domain.com/images.

An Alias solves that issue, and domain.com/redir/images will be interpreted by the server as /images, however I also need to apply some RewriteRules, which as you say won’t work because the Alias overrides them. So it’s a case of the php include allowing RewriteRules but breaking due to path translation issues, or an Alias that doesn’t have path translation issues but doesn’t allow RewriteRules.

What do you mean by “absolute redirect”? I googled this but couldn’t figure it out.

Kadence,

An absolute redirect is either external (http://domain/path/to/file) or internal (/path/to/file). This is differentiated from relative links as they are subject to the (perceived) location of the file being accessed. Since mod_rewrite can often change the directory level, absolute URLs are necessary (if you don’t use the <base> tag).

Regards,

DK

Thank you, but I don’t really follow :confused:

The links I am trying to rewrite are absolute links - e.g. if the user1 account’s domain is domain1.com, then in the HTML what I’m trying to rewrite is linked as [I]http://www.domain1.com/redir/test.php[/I]. And the rewrite rule would be something like as above,

RewriteRule redir/test.php /home/user2/public_html/test.php

I thought perhaps you mean an absolute link in the RewriteRule, so I alternatingly attempted each of the following as well:

RewriteRule http://www.domain1.com/redir/test.php /home/user2/public_html/test.php
RewriteRule http://www.domain1.com/redir/test.php http://www.domain1.com/home/user2/public_html/test.php
RewriteRule redir/test.php http://www.domain1.com/home/user2/public_html/test.php

Though none of those made a difference.

What specifically would I have to modify to get this to work?

Kad,

mod_rewrite deals with HTTP requests so the internal absolute path is relative to DocumentRoot, NOT the server’s physical path (error #1). NEVER use the http: protocol in a RewriteRule’s regex (because a RewriteRule attempts to match the {REQUEST_URI} string, not the entire URL; error #2).

Regards,

DK

Yes I knew that was likely not going to do anything productive and was experimenting just in case, but I am just unclear on what you are getting at in terms of what the solution here is…?

Kad,

As above, my solution is that there is no solution (unless you go “over the top” and use a domain which encompasses the subdomains/subaccounts). You just can’t get there from here!

Regards,

DK

I see - unfortunately that means a lot of extra work for me then :frowning:

What about Aliases and regex - can one use regex with Aliases?

Kad,

:agree:

Some of them - see the link I’d provided (RedirectMatch).

Regards,

DK

Thank you. I just read about the AliasMatch directive, which I think is exactly what I need.

Having another issue with that though where it displays the directory listing instead of the actual content, but I think I should make a separate thread for that. Thanks again :slight_smile:

:tup:

Regards,

DK