How to rewrite a URL with htaccess?

How to rewrite this URL with htaccess

http://domain.com/subDomain/file.ext?anything

to

http://subDomain.domain.com/file.ext?anything

and return a 301 code?

Thanks.


Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\\.com$ [NC]
RewriteRule ^subDomain(.*)$ http://subDomain.domain.com$1 [L,R=301]

Thanks but it does not work, no redirection happens

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteCond %{HTTP_HOST} ^cxxxxxxxxxxxxc\\.com$ [NC]
RewriteRule ^axxxa(.*)$ http://axxxa.cxxxxxxxxxxxxc.com$1 [L,R=301]

Besides, the RewriteCond is not all

http://domain.com/anything

but only

http://domain.com/subDomain/anything

At a glance, it looks like ScallioXTX’s code should work. What’s the URL you’re typing in the address bar? Is there anything else in your htaccess file?

What do you mean by that!? The RewriteCond must only contain the host, no part of the URL path whatsoever.

First off, your code works! Thanks. I’m just now writing up why it’s not working for me. I’ll post it soon.

I’m a GUI kind of guy, I really dislike regex, command line, unix, rewrite and stuff like that. I do it only when I must. What I meant was not the syntax of RewriteCond but my logic, I need to redirect only

http:mydomain/sub-domain/anything-here

Sorry for the confusion.

Great question! ScallioXTX’s code does work, the problem is the second .htaccess file.

I have a main domain and a sub-domain (there will be many more)

http://condominiospcc.com/
http://avila.condominiospcc.com/

This is on a shared LAMP server. In the file structure the sub-domain is a directory (folder) inside public_html

/home/user/public_html/avila/

The domain has an htaccess file as does each subdomain, they are mostly identical except for the redirection I’m trying to set up.

Apache looks at all the htaccess files. When a visitor uses the sub-domain, Apache only looks at the htaccess file in the sub-domain and everything works fine. When the visitor does not use the sub-domain Apache first looks in the public_html folder and then in the sub-domain folder. The redirecton stops working when Apache reaches this code in the sub-domain’s htaccess file.



# other stuff up to here no problem

RewriteEngine on

# defeat robot exploits
RewriteCond %{QUERY_STRING} http:// [OR]
RewriteCond %{REQUEST_URI} http:// [OR]
RewriteCond %{QUERY_STRING} http%3A%2F%2F
RewriteRule ^(.*)$ - [F]

# defeat robot exploits
RewriteCond %{QUERY_STRING} DECLARE%20@S%20CHAR [OR] 
RewriteCond %{QUERY_STRING} SET%20@S=CAST
RewriteRule ^(.*)$ - [F]

I wrote code to do the redirection with php but I was hoping to be able to do it with htaccess.

Which .htaccess did you put it in? I’d recommend putting it at the very top of the .htaccess in the public_html/ (i.e., not in the subdomain directory). That should work.

That’s how I had it and it does not work. I tried putting it in the sub-directory and it does not work either.

:frowning:

If you have rewrite rules in the avila htaccess, then those will override the rewrite rules in the public_html htaccess. Your best bet is to keep all your rules in just one htaccess file – the public_html htaccess file – and use conditions for anything that you want to apply to only one particular domain.

If things worked according to plan visitors to the main domain would use the main domain and visitors to the sub-domains would use the sub-domains. I’m creating a condo management site, the main domain belongs to the administrator and each building has it’s own sub-domain. This works perfectly well. Each building can have it’s own styling, etc., etc., etc.

The issue is that a visitor can get to a building sub-domain through the main domain (domain/subDomain/) and then the server variable has the wrong “HTTP_HOST” and this throws a monkey wrench into my code. I could manage that but I want visitors to come in through the front door which is the reason for the code 301.

If the visitor goes to a sub-domain then the public_html htaccess is not read by Apache which is the reason I need the htaccess files in public_html and in each sub-domain.

I wrote some php code (which seems to have vanished) to do the redirect and if I can’t get htaccess to do it I’ll have to use it.

The reason things are getting a bit tricky is because the public root of each subdomain is nested inside the public root of the main domain. So when someone accesses domain.com/avila, that’s a real directory they’re accessing, with its own htaccess. I see two solutions that would make your site’s behavior more simple and predicable.

  1. The subdomains don’t get to have htaccess files, and all rules must reside in the public_html/.htaccess file.

Or 2) Un-nest the subdomains from the main domain. Change your filesystem directory stucture to something like this:

main_domain/public_html/
avila/public_html/
other_subdomain/public_html/

Jeff:

I mentioned that I’m using a shared LAMP server. I don’t have control over the file structure of sub-domains and if a visitor arrives via sub-domain URL then the public_html htaccess file is not read.

I just redid the rewrite code

if($_SERVER['HTTP_HOST'] == 'myDomain.com') {
    $domain = 'subDomain.myDomain.com';
    $protocol = $_SERVER['SERVER_PROTOCOL'];
    header("{$protocol} 301 Moved permanently");
    header("Location: http://{$domain}/");
    exit;
}

Unfortunately the public_html htaccess will also not be read (-ish) if the user requests the subdomain directory (that is, domain.com/avila). That’s why the redirect isn’t working. So it looks like you’re stuck with either option 1 above, or your PHP solution (or switching to a different host :wink: ).

Wrong! Both htaccess files are read (public_html and avila). When I remove the rewrite stuff from the avila sub-domain and leave on the one in public_html htaccess, the redirect works. It doesn’t like rewrite stuff in both htaccess files.

… or switching to a different host :wink:

No way! I love my hosting service. I’ve been with them for just over ten years with hardly a problem. Great uptime and great help-desk which I need to use very seldom. I had two crummy hosts before this one and I’m not risking another crummy one.

:nanaman:

Yes, I know. I mentioned that behavior in post #10 above. In my last post, I tried to simplify the explanation with an “-ish”, but I suppose I’ll be more explicit this time.

When either domain.com/avila or avila.domain.com is accessed, then both htaccess files are read. But if you have rewrite rules in both, then the avila rewrites will override the public_html rewrites. That’s why it appears as if the public_html htaccess is not read.

Then surely if you put the redirect in the htaccess in the avila subdirectory it should work, no?

::slaps forehead::

You’re absolutely right. :slight_smile: And it would require only minor modification from what you originally posted.

Instead of

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule [1]subDomain/COLOR$ http://subDomain.domain.com$1 [L,R=301]

it would be

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://subDomain.domain.com$1 [L,R=301]


  1. COLOR=“#FF0000↩︎

Isn’t there a rule that tells Apache to end reading/executing the htaccess commands?

-ish. :slight_smile:

Rewrite rules have a “last” flag, which tells Apache to not apply any more rewrites. BUT, after a rewrite has changed the URI, then Apache will re-process that URI as if it were a brand new request, which means the rewrite phase will execute again. UNLESS the rewrite resulted in an external redirect, in which case it will just send the redirect response.

ALSO, the “last” flag only applies to rewrite rules. Other directives such as DirectoryIndex, Options, Redirect, etc., from both htaccess files will still be processed.

There’s a lot of nuance here, and if you want to be certain you’ve got all the details, there’s no better place than the official documentation. You can read about [URL=“https://httpd.apache.org/docs/2.4/howto/htaccess.html#how”]how htaccess directives are applied, and about [URL=“https://httpd.apache.org/docs/2.4/rewrite/tech.html#InternalRuleset”]how rewrite rules are processed.