Htaccess rewrite rule

Could someone please point me in the right direction.

I want to create a rewrite rule so that if a user accesses the site on www.foo.com it would request www.foo.com/foo/index.php

So basically if they come through www.foo.com all of the php pages would be forced to go through the foo php controller. However if they accessed www.foo.co.uk they wouldn’t.

With the rewrite rule
www.foo.com/testpage would go to www.foo.com/foo/testpage
www.foo.com/about-us would go to www.foo.com/foo/about-us
www.foo.com/contact-us would go to www.foo.com/foo/contact-us

www.foo.co.uk/testpage would go to www.foo.co.uk/testpage
www.foo.co.uk/about-us would go to www.foo.co.uk/about-us
www.foo.co.uk/contact-us would go to www.foo.co.uk/contact-us

Of course the url in the address bar must stay www.foo.com/testpage, www.foo.com/about-us etc not not display the middle controller

Cheers

I’d preventing the rewriting of static files through adding an extra RewriteCond


RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\\.foo\\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/foo
[COLOR="Blue"]RewriteCond %{REQUEST_URI} !(gif|jpg|jpeg|png|css|js)$[/COLOR]
RewriteRule (.*) foo/$1 [L]

That prevents requests to jpg, jpeg, png, css and js files from being rewritten. You can expand the list as you see fit :slight_smile:

Hey thanks guys,

With the following code worked


RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\\.foo\\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/foo
RewriteRule (.*) foo/$1 [L]

But how do I get it to only do this with php scripts. The reason being is that I have aliased a website.
http://www.foobar.com is the main website and http://www.foo.com is the alias.

At the moment the css and js points to “/css/style.css” and “/js/myScript.js”.

I know if I make these links full to my main domain (http://www.foobar.com/css/style.css) it will work but I didn’t really want to do this. Aswell as this all the images would need to be fully linked so that would be a major pain

You’re right, my explanation of the [L] flag was incorrect.
However, I tested your code and what you said also doesn’t seem to be the case

I put that in a .htaccess to test it, and ended up at /baz (used real values instead of the example placeholders of course).

I did some digging, and in the manual it says for the last flag:

Remember, however, that if the RewriteRule generates an internal redirect (which frequently occurs when rewriting in a per-directory context), this will reinject the request and will cause processing to be repeated starting from the first RewriteRule.

It would seem the [L] flag terminates the current rewrite “round”, does an internal redirect which starts a new “round”, and this process is repeated until no more rewrites are done (i.e., input url isn’t changed by any of the rules) or until a certain amount of “rounds” has been gone through (defaults to 10 I read somewhere).

To be honest, the [L] flag always puzzles me a bit. I know I should use it, and I always do, but I’m not 100% sure why.

On second thought, the code I posted earlier creates an infinite loop :x

You need an extra RewriteCond (and the dots in the domain name need to be escape, i.e. prepended with a \):


RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\\.foo\\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/foo
RewriteRule (.*) foo/$1 [L]

Actually, the L flag is to terminate further rewriting if you have more rules after the current one.

Example:


RewriteRule ^foo$ /bar [L]
RewriteRule ^bar$ /baz

example.com/foo will be rewritten to example.com/bar and it will stop at it. Without the L it would process the next rule which would rewrite example.com/bar to example.com/baz.

RewriteCond (can be multiple) only applies to the first RewriteRule following it.

Ok, what you want to do requires several steps.
First of all, you need a .htaccess file placed in the root of the website.
In this file you first need to tell apache you want to use the rewrite engine:


RewriteEngine On

You then need to tell to apache to do something if the domain is [noparse]www.example.com[/noparse], sort of like “if domain is [noparse]www.example.com[/noparse], do the following”:


RewriteCond %{HTTP_HOST} ^www.example.com$

Then you need to tell it that is this is case, all requests to [noparse]www.example.com/[/noparse]whatever must be sent to [noparse]www.example.com/foo/[/noparse]whatever :


RewriteRule (.*) /foo/$1 [L]

The [L] flag is to terminite the “if statement” started by the RewriteCond.

And your done. Combined code for .htaccess:


RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule (.*) /foo/$1 [L]

You don’t have to set anything for example.co.uk, as they can just be accessed like nothing ever changed.


RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.foo.com$
RewriteRule (.*) /foo/$1