Forum Moderators: phranque
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co.uk$
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]
However, when I add this, the code works but all formatting on the site is lost , like it isn;t loading the stylesheet and some of the pages using querystrings don't load at all.
Any ideas?
[edited by: jdMorgan at 2:57 pm (utc) on Nov. 30, 2009]
[edit reason] example.co.uk [/edit]
To explain, I suspect that your URL-to-filespace mapping may not be direct, and that you may need to change the pattern or add a RewriteBase directive pointing to a non-default directory.
This problem may also arise if this .htaccess code is located in a subdirectory-URL of the site, and not in example.com/.htaccess, or if the links to these resources are built dynamically, and something's wrong with the script method used to build the links.
But these are just guesses; Looking at both of those log files will likely give you a way forward.
Jim
This problem may also arise if this .htaccess code is located in a subdirectory-URL of the site, and not in example.com/.htaccess, or if the links to these resources are built dynamically, and something's wrong with the script method used to build the links.
That was the problem.
Thanks.
I put it into the root and it works correctly now.
I tried adding this as 2 lines before the above rewrite rule but it still fails:
RewriteCond %{HTTP_HOST} ^mysite.co.uk/cart/admin$
RewriteRule (.*) [mysite.co.uk...] [R=301,L]
Although I don't believe this code would do what you intend it to do, it certainly won't do anything with a URL-path pattern appended to the hostname pattern; That path-part pattern belongs in the RewriteRule pattern instead.
It sounds to me rather like you want to add an exception to your existing non-www to www redirect rule, so that "admin" URLs are *not* redirected to "www". This is typically done with a RewriteCond examining (with a negative match) either $1 (the back-reference to the URL-path matching your RewriteRule pattern), or the server variable %{REQUEST_URI}, as in,
RewriteCond $1 !^admin/
RewriteCond %{REQUEST_URI} !^/admin/
Jim
I tired this but it fell over:
RewriteEngine On
#
RewriteCond $1 !^admin/
RewriteRule (.*) http://example.co.uk/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} ^example.co.uk$
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]
[edited by: jdMorgan at 9:35 pm (utc) on Nov. 30, 2009]
[edit reason] example.co.uk [/edit]
If you still want to use two rules, then they'd be;
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^example\.co\.uk [NC]
RewriteCond $1 !^admin/
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} ^www\.example\.co\.uk [NC]
RewriteRule ^admin/(.*)$ http://example.co.uk/admin/$1 [R=301,L]
That's not a good reason. If the old rule redirects those "example.co.uk/admin" requests, then you do not (indeed, cannot) add another rule to redirect them back, because that would cause an 'infinite' redirection loop.
Instead, you correct the old rule, so that the unwanted redirect on /admin requests doesn't happen in the first place -- as I did above.
It is generally a mistake to correct a coding deficiency by immediately adding more code. At worst, this won't completely fix the underlying problem, leaving you with an even more subtle (or hidden) error to deal with. At best, it bloats the code and slows down the machine. Hmmm... I'm reminded of a certain PC operating system when I say this, for some reason... ;)
Jim