Forum Moderators: phranque

Message Too Old, No Replies

formatting lost on mod rewrite

         

jackvull

11:55 am on Nov 30, 2009 (gmt 0)

10+ Year Member



I am trying to use this in an htaccess file to redirect all non www URLs to www:

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]

jdMorgan

2:58 pm on Nov 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Take a look at your raw server access log file *and* your raw server error log file; they likely contain a 'big hint' as to the problem. Particularly, look at the filepath that the server is resolving a css stylesheet link to: Does it contain an "extra" directory level, or is one missing?

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

jackvull

3:16 pm on Nov 30, 2009 (gmt 0)

10+ Year Member




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.

jackvull

4:06 pm on Nov 30, 2009 (gmt 0)

10+ Year Member



Next issue.
I want all access to go via www.
However, I have some admin pages that need to be accessed by me specifically on the [mysite...] domain

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]

jdMorgan

5:14 pm on Nov 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



HTTP_HOST contains *only* the requested hostname, and possibly a trailing period (for an FQDN) ot a port number, or both -- for example, "www.example.com.:80". It does not contain any part of the URL-path.

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/

-or-

RewriteCond %{REQUEST_URI} !^/admin/

Note that the first RewriteCond's pattern contains no leading slash, while the second one's must, and that "!" means "NOT".

Jim

jackvull

5:50 pm on Nov 30, 2009 (gmt 0)

10+ Year Member



Shouldn't I take out the !
ie I want every URL with the word admin in the be redirected to http://example.com
whereas every URL with normal pages (index.php, etc.)
I want redirected to http://www.example.com

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]

jdMorgan

9:37 pm on Nov 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why redirect /admin at all? You should request it only from example.co.uk, not from www.example.co.uk, and I don't imagine you'll be publicly linking to it.

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]

Jim

jackvull

6:53 am on Dec 1, 2009 (gmt 0)

10+ Year Member



because when I request it from example.co.uk, the old rule redirects it to www messing up some of the sessions

g1smd

10:06 am on Dec 1, 2009 (gmt 0)

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



Beware of end anchoring domain names in patterns.

If you do, the pattern will not match

example.co[b]m:80[/b]
or
example.co[b]m.[/b]
(with trailing period) both of which are valid.

jdMorgan

12:29 pm on Dec 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> because when I request it from example.co.uk, the old rule redirects it to www messing up some of the sessions

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